netlink 套接字问题:内核冻结

发布于 2024-11-05 05:03:56 字数 1783 浏览 2 评论 0原文

我正在尝试使用 netlink 套接字在用户空间和内核空间之间交换消息...我从用户空间向内核空间发送一条消息,一切正常,但是当我尝试从内核空间回复时,系统冻结。特别是,我使用工作队列安排一个函数,该函数创建消息并使用 netlink_unicast 函数发送到用户空间...这里有一些内核代码:

void wq_func(struct work_queue *wq)
{
  struct sk_buff *resp = alloc_skb(NLMSG_LENGTH(100), GFP_KERNEL);
  if (!resp)
  {
     printk(KERN_INFO "alloc_skb failed");
     return;
  }

  struct nlmsghdr *nlh = (struct nlmsghdr *)skb_put(resp, NLMSG_LENGTH(100));
  memset(nlh, 0, NLMSG_LENGTH(100));
  nlh->nlmsg_len = NLMSG_LENGTH(100);
  nlh->nlmsg_pid = 0;
  nlh->nlmsg_flags = 0;
  strcpy(NLMSG_DATA(nlh), "From kernel: Yes i'm here!");
  NETLINK_CB(resp).pid = 0;
  NETLINK_CB(resp).dst_group = 0;

  printk(KERN_INFO "Trying to send a netlink message to pid %d", pid);
  int err = netlink_unicast(s, resp, pid, MSG_DONTWAIT);
  if (err < 0)
    printk(KERN_ALERT "Error sending message to user-space");
  kfree_skb(resp);
}
DECLARE_WORK(wq, wq_func);

static void input_nl(struct sk_buff *buff)
{
  printk(KERN_INFO "Received message socket NETLINK_TEST");
  if (buff == NULL)
  {
    printk(KERN_ALERT "NULL sk_buff!");
    return;
  }
  struct nlmsghdr *nlh = (struct nlmsghdr *)buff->data;
  printk(KERN_INFO "Received netlink message from pid %d: %s", nlh->nlmsg_pid,       NLMSG_DATA(nlh));

  pid = nlh->nlmsg_pid;

  schedule_work(&wq);
}

int __init knl_init()
{
  printk(KERN_INFO "knl module loaded");

  s = netlink_kernel_create(&init_net, NETLINK_TEST, 0, input_nl, NULL, THIS_MODULE);

  if (s == NULL)
    return -1;
  return 0;
}

如果我尝试注释对 netlink_unicast 内核的调用不会冻结。从用户空间我可以正确发送消息。我记得同样的代码在过去运行良好,现在我对这个奇怪的错误感到非常惊讶。

有什么想法吗?

谢谢大家!


我尝试在 netlink_unicast 调用之后删除 kfree_skb 调用并且一切正常...那么,为什么系统会因该调用而挂起?我应该在哪里释放分配的sk_buff?

I'm trying to use netlink sockets to exchange messages between user-space and kernel space...i send a message from user-space to kernel-space and all works well but when i try to reply from kernel-space, system freezes. In particular i schedule with a workqueue a function that create the message and send to user-space using netlink_unicast function...here some kernel code:

void wq_func(struct work_queue *wq)
{
  struct sk_buff *resp = alloc_skb(NLMSG_LENGTH(100), GFP_KERNEL);
  if (!resp)
  {
     printk(KERN_INFO "alloc_skb failed");
     return;
  }

  struct nlmsghdr *nlh = (struct nlmsghdr *)skb_put(resp, NLMSG_LENGTH(100));
  memset(nlh, 0, NLMSG_LENGTH(100));
  nlh->nlmsg_len = NLMSG_LENGTH(100);
  nlh->nlmsg_pid = 0;
  nlh->nlmsg_flags = 0;
  strcpy(NLMSG_DATA(nlh), "From kernel: Yes i'm here!");
  NETLINK_CB(resp).pid = 0;
  NETLINK_CB(resp).dst_group = 0;

  printk(KERN_INFO "Trying to send a netlink message to pid %d", pid);
  int err = netlink_unicast(s, resp, pid, MSG_DONTWAIT);
  if (err < 0)
    printk(KERN_ALERT "Error sending message to user-space");
  kfree_skb(resp);
}
DECLARE_WORK(wq, wq_func);

static void input_nl(struct sk_buff *buff)
{
  printk(KERN_INFO "Received message socket NETLINK_TEST");
  if (buff == NULL)
  {
    printk(KERN_ALERT "NULL sk_buff!");
    return;
  }
  struct nlmsghdr *nlh = (struct nlmsghdr *)buff->data;
  printk(KERN_INFO "Received netlink message from pid %d: %s", nlh->nlmsg_pid,       NLMSG_DATA(nlh));

  pid = nlh->nlmsg_pid;

  schedule_work(&wq);
}

int __init knl_init()
{
  printk(KERN_INFO "knl module loaded");

  s = netlink_kernel_create(&init_net, NETLINK_TEST, 0, input_nl, NULL, THIS_MODULE);

  if (s == NULL)
    return -1;
  return 0;
}

If i try to comment the call to netlink_unicast kernel doesn't freeze. From user-space i can send a message correctly. I remember that the same code worked well in the past and i 'm very surprised about this strange error now.

Any idea?

Thank you all!


I tried to remove kfree_skb call after netlink_unicast call and all works...so, why the system hangs with that call? Where should i free allocated sk_buff?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拿命拼未来 2024-11-12 05:03:56

netlink_unicast() 获取 skb 的所有权并释放它本身。

netlink_unicast() takes ownership of the skb and frees it itself.

居里长安 2024-11-12 05:03:56

调用 netlink_unicast 后,您无法释放 struct sk_buff

After calling netlink_unicast you cannot free struct sk_buff

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文