LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 07-16-2014, 03:35 AM   #1
poplinux
Member
 
Registered: May 2012
Posts: 111

Rep: Reputation: Disabled
netlink question in kernel space.


Dear Sir.

Now, I'm try to study netlink subsystem in linux kernel.(Ver 2.6.32.27)

And, I'm found very useful size, See below.

http://www.linuxfoundation.org/colla..._netlink_howto

So. I'm create two kernel module.

new_netlink_family.ko : add new netlink family to kernel.
example_on_kernel.ko : Send netlink msg to new netlink family.

When insmod example_on_kernel.ko, I'm want to see "doc_exmpl_echo() : called"

But, I'm can't see this message.

Please help me.


file name : new_netlink_family.c
Code:
  1 #include <linux/kernel.h>
  2 #include <linux/init.h>
  3 #include <net/genetlink.h>
  4 #include "new_netlink_family.h"
  5 
  6 
  7 /* handler */
  8 static int doc_exmpl_echo(struct sk_buff *skb,
  9                           struct genl_info *info)
 10 {
 11   printk(KERN_ALERT "%s() : called\n", __FUNCTION__);
 12   return 0;
 13 };
 14 
 15 /* Operation definition */
 16 static struct genl_ops doc_exmpl_gnl_ops_echo = {
 17   .cmd = DOC_EXMPL_C_ECHO,
 18   .flags = 0,
 19   .policy = doc_exmpl_genl_policy,
 20   .doit = doc_exmpl_echo,
 21   .dumpit = NULL,
 22 };
 23 
 24 
 25 static int new_netlink_family_init(void)
 26 {
 27   int ret;
 28 
 29   printk(KERN_ALERT "%s() : called\n", __FUNCTION__);
 30 
 31   ret = genl_register_family(&doc_exmpl_gnl_family);
 32   if (ret != 0){
 33     printk(KERN_ALERT "%s() : genl_register_family() fail\n", __FUNCTION__);
 34     goto FAILURE;
 35   }
 36 
 37   ret = genl_register_ops(&doc_exmpl_gnl_family, &doc_exmpl_gnl_ops_echo);
 38   if (ret != 0){
 39     printk(KERN_ALERT "%s() : genl_register_ops() fail\n", __FUNCTION__);
 40 
 41     ret = genl_unregister_family(&doc_exmpl_gnl_family);
 42     goto FAILURE;
 43   }
 44 
 45   return 0;
 46 
 47 FAILURE:
 48   return -ENOMEM;
 49 }
 50 
 51 void example_netlink_exit(void)
 52 {
 53   int ret;
 54 
 55   ret = genl_unregister_ops(&doc_exmpl_gnl_family, &doc_exmpl_gnl_ops_echo);
 56   ret = genl_unregister_family(&doc_exmpl_gnl_family);
 57 }
58 
 59 
 60 
 61 module_init(new_netlink_family_init);
 62 module_exit(example_netlink_exit);
 63 MODULE_LICENSE("Dual BSD/GPL");

file name : new_netlink_family.h
Code:
  1 #ifndef __NEW_NETLINK_FAMILY_H__
  2 #define __NEW_NETLINK_FAMILY_H__
  3 
  4 /* attributes */
  5 enum {                    
  6   DOC_EXMPL_A_UNSPEC,
  7   DOC_EXMPL_A_MSG,
  8   __DOC_EXMPL_A_MAX,
  9 };
 10 
 11 #define DOC_EXMPL_A_MAX (__DOC_EXMPL_A_MAX-1)
 12 
 13 /* attribute policy */
 14 static struct nla_policy doc_exmpl_genl_policy[DOC_EXMPL_A_MAX+1] = {
 15   [DOC_EXMPL_A_MSG] = {.type = NLA_NUL_STRING},
 16 };
 17   
 18 
 19 /* family defination */
 20 static struct genl_family doc_exmpl_gnl_family = {
 21   .id = GENL_ID_GENERATE,
 22   .hdrsize = 0,
 23   .name = "DOC_EXMPL",
 24   .version = 1,
 25   .maxattr = DOC_EXMPL_A_MAX,
 26 };
 27   
 28   
 29 /* commands */
 30 enum {
 31   DOC_EXMPL_C_UNSPEC,
 32   DOC_EXMPL_C_ECHO,
 33   __DOC_EXMPL_C_MAX,
 34 };
 35     
 36 #define DOC_EXMPL_C_MAX (__DOC_EXMPL_C_MAX -1)
 37     
 38 #endif /* __NEW_NETLINK_FAMILY_H__ */

file name : example_on_kernel.c
Code:
 1 #include <net/netlink.h>
  2 #include <net/genetlink.h>
  3 
  4 #include "new_netlink_family.h"
  5 
  6 #if 0
  7 /* family defination */
  8 static struct genl_family doc_exmpl_gnl_family = {
  9   .id = GENL_ID_GENERATE,
 10   .hdrsize = 0,
 11   .name = "DOC_EXMPL",
 12   .version = 1,
 13   .maxattr = DOC_EXMPL_A_MAX,
 14 };
 15 #endif
 16 
 17 static int __msg_test(const char *msg)
 18 {
 19   int rc;
 20   void *msg_head = NULL;
 21   struct sk_buff *skb = NULL;
 22 
 23   printk(KERN_ALERT "%s() : called\n", __FUNCTION__);
 24 
 25   skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
 26   if (skb == NULL){
 27     printk(KERN_ALERT "%s() : genlmsg_new() fail\n", __FUNCTION__);
 28     rc = -ENOMEM;
 29     goto FAILURE;
 30   }
 31 
 32   /* Create the message headers */
 33   msg_head = genlmsg_put(skb, 0, 0, &doc_exmpl_gnl_family, 0, DOC_EXMPL_C_ECHO);
 34   if (msg_head == NULL){
 35     printk(KERN_ALERT "%s() : genlmsg_put() fail\n", __FUNCTION__);
 36     rc = -ENOMEM;
 37     goto FAILURE;
 38   }
 39 
 40   /* add a DOC_EXMPL_A_MSG attribute */
 41   rc = nla_put_string(skb, DOC_EXMPL_A_MSG, msg);
 42   if (rc != 0){
 43     printk(KERN_ALERT "%s() : nla_put_string() fail\n", __FUNCTION__);
 44     rc = -ENOMEM;
 45     goto FAILURE;
 46   }
 47 
 48   /* finalize the message */
 49   rc = genlmsg_end(skb, msg_head);
 50   if (rc < 0){
 51     printk(KERN_ALERT "%s() : genlmsg_end() fail\n", __FUNCTION__);
 52     rc = -ENOMEM;
 53     goto FAILURE;
 54   }
 55 
 56   rc = genlmsg_unicast(&init_net, skb, 0);
 57   if (rc != 0){
 58     printk(KERN_ALERT "%s() : genlmsg_unicast() fail\n", __FUNCTION__);
 59     rc = -ENOMEM;
 60     goto FAILURE;
 61   }
 62 
 63   return 0;
 64 
 65 FAILURE:
 66   /* skb free api 필요함 */
 67   nlmsg_free(skb);
 68   return rc;
 69 };
 70 
 71 int example_on_kernel_init(void)
 72 {
 73   int ret = 0;
 74 
 75   printk(KERN_ALERT "%s() : called\n", __FUNCTION__);
 76 
 77   ret = __msg_test("BABO");
 78   if (ret < 0){
 79     printk(KERN_ALERT "%s() : __msg_test is fail [ %d ]\n", __FUNCTION__, ret);
 80     return -ENOMEM;
 81   }
 82 
 83   return 0;
 84 }
 85 
 86 void example_on_kernel_exit(void)
 87 {
 88 }
 89 
 90 module_init(example_on_kernel_init);
 91 module_exit(example_on_kernel_exit);
 92 
 93 MODULE_LICENSE("Dual BSD/GPL");
Attached Files
File Type: txt example_on_kernel.c.txt (1.8 KB, 11 views)
File Type: txt new_netlink_family.c.txt (1.3 KB, 9 views)
File Type: txt new_netlink_family.h.txt (704 Bytes, 9 views)
 
Old 07-20-2014, 02:21 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Moved: This thread is more suitable in Linux-Kernel and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel Panic when netlink message is sent from User space to kernel space itrilok Programming 1 11-28-2012 10:56 AM
[SOLVED] Netlink sockets in kernel and ethernet driver. Kernel crash. Squier Programming 2 05-08-2011 12:57 PM
a question about netlink socket? b0207191 Linux - Kernel 0 12-16-2010 08:56 PM
Netlink Kernel User-space communication code for 2.6.33, 2.6.34+ Kernels kiran_cyberpro Programming 0 06-09-2010 03:19 AM
Netlink sockets usage to communicate b/w user and kernel space. vinodkr83 Linux - Newbie 1 07-03-2009 03:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 08:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration