gpt4 book ai didi

c - Netlink 导致内核 panic

转载 作者:行者123 更新时间:2023-11-30 15:33:01 26 4
gpt4 key购买 nike

我试过this program 。该程序向内核发送“hello”,内核再次向用户空间回复“hello”一次。

我的要求:用户必须发送“hello”并且必须每2秒接收一次消息。

我已经尝试过了。

用户应用:添加了发送和接收循环

#define NETLINK_USER 31    
#define MAX_PAYLOAD 1024 /* maximum payload size*/

struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
struct msghdr msg;

int main()
{
sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
if(sock_fd<0)
return -1;

memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */

bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));

memset(&dest_addr, 0, sizeof(dest_addr));
memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */

nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;

strcpy(NLMSG_DATA(nlh), "Hello");

iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
while(1) { // <--- loop here. only works once.
sleep(2);
printf("Sending message to kernel\n");
sendmsg(sock_fd,&msg,0);

recvmsg(sock_fd, &msg, 0);
printf("Received message payload: %s\n", (char *)NLMSG_DATA(nlh));
}

close(sock_fd);
}

内核模块:没有改变任何东西。

#define NETLINK_USER 31
struct sock *nl_sk = NULL;

static void hello_nl_recv_msg(struct sk_buff *skb)
{
struct nlmsghdr *nlh;
int pid;
struct sk_buff *skb_out;
int msg_size;
char *msg="Hello from kernel";
int res;

printk(KERN_INFO "Entering: %s\n", __FUNCTION__);

msg_size=strlen(msg);

nlh=(struct nlmsghdr*)skb->data;
printk(KERN_INFO "Netlink received msg payload:%s\n",(char*)nlmsg_data(nlh));
pid = nlh->nlmsg_pid; /*pid of sending process */

skb_out = nlmsg_new(msg_size,0);

if(!skb_out)
{
printk(KERN_ERR "Failed to allocate new skb\n");
return;
}
nlh=nlmsg_put(skb_out,0,0,NLMSG_DONE,msg_size,0);
NETLINK_CB(skb_out).dst_group = 0; /* not in mcast group */
strncpy(nlmsg_data(nlh),msg,msg_size);

res=nlmsg_unicast(nl_sk,skb_out,pid);

if(res<0)
printk(KERN_INFO "Error while sending bak to user\n");
}

static int __init hello_init(void)
{
struct netlink_kernel_cfg cfg = {
.input = hello_nl_recv_msg
};

nl_sk = netlink_kernel_create(&init_net, NETLINK_USER, &cfg);

printk("Entering: %s\n",__FUNCTION__);

if(!nl_sk)
{
printk(KERN_ALERT "Error creating socket.\n");
return -10;
}
return 0;
}

static void __exit hello_exit(void)
{
printk(KERN_INFO "exiting hello module\n");
netlink_kernel_release(nl_sk);
}

module_init(hello_init);
module_exit(hello_exit);

此代码仅适用于第一个循环。下次内核崩溃并在黑屏中出现内核 panic 错误时。每次我重新启动时。为什么它会引起内核 panic ?我需要修改哪里?我认为问题出在内核模块中。
内核:3.13.0-24-generic

最佳答案

嗯,我认为问题出在内核模块中。但问题出在用户模块上。我花了三天时间才发现这个问题。问题出在

while(1) {           
sleep(2);
printf("Sending message to kernel\n");
sendmsg(sock_fd,&msg,0); // In 2nd iteration msg value chaged

recvmsg(sock_fd, &msg, 0); // <--- msg will update
printf("Received message payload: %s\n", (char *)NLMSG_DATA(nlh));
}

此循环仅有效一次。那是真实的。使用 recvmsg 接收消息后,msg 变量被更新,我使用该变量再次发送。这是因为代码过于智能,(源和目标)都引用相同的指针。

通过为 sendmsgrecvmsg 编写不同的变量将解决该问题。

int main()
{
int seq_no = 1;
sock_fd=socket(PF_NETLINK, SOCK_RAW, NETLINK_USER);
if(sock_fd<0)
return -1;

memset(&src_addr, 0, sizeof(src_addr));
src_addr.nl_family = AF_NETLINK;
src_addr.nl_pid = getpid(); /* self pid */

bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));

memset(&dest_addr, 0, sizeof(dest_addr));
dest_addr.nl_family = AF_NETLINK;
dest_addr.nl_pid = 0; /* For Linux Kernel */
dest_addr.nl_groups = 0; /* unicast */

nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
nlh->nlmsg_pid = getpid();
nlh->nlmsg_flags = 0;

strcpy(NLMSG_DATA(nlh), "Hello");

iov.iov_base = (void *)nlh;
iov.iov_len = nlh->nlmsg_len;
msg.msg_name = (void *)&dest_addr;
msg.msg_namelen = sizeof(dest_addr);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
/* receiver parameters */
nlh_in = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
iov_in.iov_base = (void *)nlh_in;
iov_in.iov_len = nlh->nlmsg_len;
msg_in.msg_iov = &iov_in;
msg_in.msg_iovlen = 1; /* end */

while(1) {
printf("sending message to kernel\n");
sendmsg(sock_fd,&msg,0);

recvmsg(sock_fd, &msg_in, 0);
printf("Received message payload: %s\n", (char *)NLMSG_DATA(nlh_in));
sleep(1);
}
free(nlh);
close(sock_fd);
}

关于c - Netlink 导致内核 panic ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23852866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com