簡體   English   中英

insmod:錯誤:無法插入模塊 kernel.ko:無效參數 - 與內核模塊命名方案相關的錯誤

[英]insmod: ERROR: could not insert module kernel.ko: Invalid parameters - Error related to naming scheme of kernel module

我正在使用 C 創建一個自定義內核模塊,以連接到我的 Ubuntu 機器上的 netfilter 操作。 但是,我遇到了圍繞 module_param 參數的問題。 插入模塊時,我試圖添加一個自定義字段,特別是這將在指定時丟棄 ICMP 流量。 代碼使用標准 make 文件編譯得很好,但是當使用 insmod 插入它時,我收到錯誤

insmod: ERROR: could not insert module kernel.ko: Invalid parameters

我正在使用命令

insmod kernel.ko dropicmp=1

從我讀過的內容來看,這應該適用於 module params 參數,但我嘗試過的任何方法都無法解決這個問題。

請在下面找到我的代碼。

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/ip.h>
#include <linux/tcp.h>

static struct nf_hook_ops nfho;
struct iphdr *iph;
struct tcphdr *tcp_header;
struct sk_buff *sock_buff;
unsigned int sport, dport;

// command line argument | called using insmod kernel_firewall.ko drop_icmp=1 
static int dropicmp = 1;

module_param(dropicmp, int , 0); // takes in an int from command line | (name, variable, permissions)

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 *)){

    sock_buff = skb;

    if (!sock_buff) { // if there is no socket buffer, accept
        return NF_ACCEPT;
    }

    iph = (struct iphdr *)skb_network_header(sock_buff); // using the socket buffer, create our ip header structure out of packets in it

    if (!iph) {
        printk(KERN_INFO "no ip header, dropping\n"); // self explanatory
        return NF_DROP;
    }

    if(iph->protocol==IPPROTO_TCP) {
        if(iph->saddr | 0x11000000){ // if the first prefix is in the 192 range | might need to change the if statement up | considering sprintf
            printk(KERN_INFO "192 subnet detected, dropping\n");
            return NF_DROP;
        }
        else{
            return NF_ACCEPT;
        }
    }

    if(iph->protocol==IPPROTO_ICMP) { // if ICMP

        if(dropicmp == 1){
            return NF_DROP; // drop our ICMP traffic if required
        }
        else{
            return NF_ACCEPT;
        }
    }

    return NF_ACCEPT; // default to accept

}

// initialize
static int __init initialize(void) {
    nfho.hook = hook_func;
    nfho.hooknum = NF_INET_POST_ROUTING;
    nfho.pf = PF_INET;
    nfho.priority = NF_IP_PRI_FIRST;
    nf_register_hook(&nfho);
    return 0;
}

// rmmod 
static void __exit teardown(void) {
    nf_unregister_hook(&nfho);
}

module_init(initialize);
module_exit(teardown);

這完全是由於我愚蠢的命名方案......我命名了模塊內核......顯然已經被內核使用......所以不要這樣做......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM