如题,在我的内核模块中,我尝试创建NETLINK__ROUTE 套接字来接受应用层RTM_NEWROUTE发来的消息,总是返回为NULL,一但修改成其他任意协议号,或在NETLINK_TEST自定义协议号,都可以创建成功,请问是为什么?
下面是自己写的内核模块代码,刚刚初学netlink,网上的例子很有限,自己查手册跟别人的代码写的一份,可以编译通过,流程不知道是否完善,但所能测试是否能创建套接字,就只有#define NETLINK_ROUTE 0 这个没法创建,实在是找不到原因,求助一下大家,拜托了
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <net/sock.h>
#include <linux/netlink.h>
#include <linux/err.h>
//#include <linux/rtnetlink.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("liyishen");
MODULE_DESCRIPTION("rtnetlink");
struct sock*sk = NULL;
extern struct net init_net;
static void rtnetlink_rcv(struct sk_buff *skb){
struct nlmsghdr *nlh = NULL;
char *umsg = NULL;
if(skb->len >= nlmsg_total_size(0)){
nlh = nlmsg_hdr(skb);
umsg = NLMSG_DATA(nlh);
if(umsg){
printk("kernel recv from user: %s\n",umsg);
}
}
}
static int rtnetlink_net_init(struct net *net)
{
struct sk_buff *skb;
skb = nlmsg_new(256, GFP_ATOMIC);
if(!skb)
{
printk("netlink alloc failure\n");
return -1;
}
struct netlink_kernel_cfg cfg = {
.groups = RTNLGRP_MAX,
.input = rtnetlink_rcv,
//.cb_mutex = &rtnl_mutex,
.flags = NL_CFG_F_NONROOT_RECV,
};
sk = netlink_kernel_create(net,17,&cfg);//就是这里,只要第二个参数传入NETLINK_ROUTE,就会返回NULL,其它协议都可以,甚至自定义的NETLINK_TEST也可以
if(NULL == sk){
printk("socked create failed\n");
return -1;
}
printk("rtnl sock create finished\n");
return 0;
}
int rtnl_init(void){
rtnetlink_net_init(&init_net);
return 0;
}
void rtnl_exit(void){
if(sk){
netlink_kernel_release(sk);
sk = NULL;
}
printk("rtnl_exit!\n");
}
module_init(rtnl_init);
module_exit(rtnl_exit);