簡體   English   中英

如何在內核和用戶空間之間創建“netlink”?

[英]How do I create a “netlink” between kernel and userspace?

我想使用netlink在應用程序和內核空間之間進行通信。 我的Linux內核版本是2.6.28,以下是我的錯誤代碼:

nf_sock=netlink_kernel_create(NL_PROTO,0,nl_user_skb,THIS_MODULE);

縮寫錯誤消息是:

error: too few arguments to function 'netlink_kernel_create'

在文件<linux/netlink.h> ,函數netlink_kernel_create()定義為

extern struct sock *netlink_kernel_create(struct net *net,int unit,unsigned int groups,void (*input)(struct sk_buff *skb),struct mutex *cb_mutex,struct module *module)

我不明白第一個參數net用什么。 誰能解釋一下我應該在這里使用什么?

struct net包含有關網絡命名空間的信息,網絡命名空間是一組可用於進程的網絡資源。 請注意,可能有多個網絡命名空間(即網絡堆棧的多個實例),但大多數驅動程序使用init_net命名空間。

您的電話應該看起來像下面這樣

nf_sock = netlink_kernel_create(&init_net,
                                NETLINK_USERSOCK,
                                0,
                                nl_rcv_func,
                                NULL,
                                THIS_MODULE);

其中nl_rcv_func是一個以struct sk_buff *skb為唯一參數並處理收到的netlink消息的函數。

你似乎一直在關注像這樣的指南,它(從2005年開始)可能已經被內核的開發所超越。 似乎從內核端創建netlink的內部API已經發生了變化。

檢查本地內核樹中的Documentation /文件夾以獲取一些(希望更新鮮的)文檔,或者閱讀代碼本身。 你也可以拖網 Linux內核郵件列表存檔的,似乎已經發生變化的任何提及。

是2.6.29的實際實現,如果你寧願向后解決它(當然還沒有在你自己的樹中檢查過它)。

是的,struct net確實用於net命名空間,但是總是使用init_net是不合適的,你應該注冊自己的pernet_operations,如下所示:

static struct pernet_operations fib_net_ops = {
        .init = fib_net_init,
        .exit = fib_net_exit,
};

static int __net_init fib_net_init(struct net *net)
{
        int error;

#ifdef CONFIG_IP_ROUTE_CLASSID
        net->ipv4.fib_num_tclassid_users = 0;
#endif
        error = ip_fib_net_init(net);
        if (error < 0)
                goto out;
        error = nl_fib_lookup_init(net);
        if (error < 0)
                goto out_nlfl;
        error = fib_proc_init(net);
        if (error < 0)
                goto out_proc;
out:
        return error;

out_proc:
        nl_fib_lookup_exit(net);
out_nlfl:
        ip_fib_net_exit(net);
        goto out;
}

static int __net_init nl_fib_lookup_init(struct net *net)
{
        struct sock *sk;
        struct netlink_kernel_cfg cfg = {
                .input  = nl_fib_input,
        };

        sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
        if (sk == NULL)
                return -EAFNOSUPPORT;
        net->ipv4.fibnl = sk;
        return 0;
}

最后:

register_pernet_subsys(&fib_net_ops);

我建議ioctl進行內核/用戶通信。 ioctl接口是標准的,並且內核之間更新的可能性很小。

暫無
暫無

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

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