title: Netlink giving kernel panic
body: <p>I tried <a href="https://stackoverflow.com/a/17606674/2689414">this program</a>. This program send "hello" to kernel and kernel replies "hello" to user space again for one time.</p>
  
  <p>My requirement: user has to send "hello" and have to receive message for every 2 seconds.</p>
  
  <p>What I have tried.</p>
  
  <p><strong>User app</strong>: added loopfor sending and receiving</p>
  
  <pre><code>#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&lt;0)
          return -1;
  
      memset(&amp;src_addr, 0, sizeof(src_addr));
      src_addr.nl_family = AF_NETLINK;
      src_addr.nl_pid = getpid(); /* self pid */
  
      bind(sock_fd, (struct sockaddr*)&amp;src_addr, sizeof(src_addr));
  
      memset(&amp;dest_addr, 0, sizeof(dest_addr));
      memset(&amp;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-&gt;nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
      nlh-&gt;nlmsg_pid = getpid();
      nlh-&gt;nlmsg_flags = 0;
  
      strcpy(NLMSG_DATA(nlh), "Hello");
  
      iov.iov_base = (void *)nlh;
      iov.iov_len = nlh-&gt;nlmsg_len;
      msg.msg_name = (void *)&amp;dest_addr;
      msg.msg_namelen = sizeof(dest_addr);
      msg.msg_iov = &amp;iov;
      msg.msg_iovlen = 1;
      while(1) {           // &lt;--- loop here. only works once.
          sleep(2);
          printf("Sending message to kernel\n");
          sendmsg(sock_fd,&amp;msg,0);
  
          recvmsg(sock_fd, &amp;msg, 0);
          printf("Received message payload: %s\n", (char *)NLMSG_DATA(nlh));
      }
  
      close(sock_fd);
  }
  </code></pre>
  
  <p><strong>Kernel module</strong>: didn't changed anything.</p>
  
  <pre><code>#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-&gt;data;
      printk(KERN_INFO "Netlink received msg payload:%s\n",(char*)nlmsg_data(nlh));
      pid = nlh-&gt;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&lt;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(&amp;init_net, NETLINK_USER, &amp;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);
  </code></pre>
  
  <p>This code works for first loop only. next time kernel is crashing and giving kernel panic error in black screen. Every time I'm doing reboot. Why it is giving kernel panic? Where I have to modify? I think problem is in kernel module. 
  <br>Kernel: <code>3.13.0-24-generic</code></p>