Advertisement
orzechtbg

SO2.lab10.zad1.netlink

Jun 25th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.77 KB | None | 0 0
  1. #include<linux/init.h>
  2. #include<linux/module.h>
  3. #include<linux/fs.h>
  4. #include<linux/seq_file.h>
  5. #include<linux/proc_fs.h>
  6. #include<linux/netlink.h>
  7. #include<net/netlink.h>
  8. #include<net/sock.h>
  9.  
  10. #define NETLINK_TEST 17
  11.  
  12. static int error_level;
  13. static char content[128];
  14. static struct sock *sock;
  15.  
  16. static int netlink_show(struct seq_file *seq, void *v)
  17. {
  18.     seq_printf(seq,content);
  19.     return 0;
  20. }
  21.  
  22. static int netlink_seq_open(struct inode *inode, struct file *file)
  23. {
  24.     return single_open(file,netlink_show,NULL);
  25. }
  26.  
  27. static struct file_operations netlink_proc_fops = {
  28.     .owner = THIS_MODULE,
  29.     .open = netlink_seq_open,
  30.     .read = seq_read,
  31.     .llseek = seq_lseek,
  32.     .release = single_release,
  33. };
  34.  
  35. static void netlink_receive_skb(struct sk_buff *skb)
  36. {
  37.     struct nlmsghdr *nlh;
  38.     uint32_t rlen;
  39.  
  40.     nlh = nlmsg_hdr(skb);
  41.     rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  42.     sprintf(content,"Received message: \"%s\", sequence number: %u\n",(char *)NLMSG_DATA(nlh),nlh->nlmsg_seq);
  43.     skb_pull(skb,rlen);
  44. }
  45.  
  46. static struct netlink_kernel_cfg netlink_configuration = {
  47.     .groups = 0,
  48.     .input = netlink_receive_skb,
  49.     .cb_mutex = NULL,
  50.     .compare = NULL,
  51. };
  52.  
  53. static int __init netlink_start(void)
  54. {
  55.     if(proc_create("netlink",0,NULL,&netlink_proc_fops)==NULL) {
  56.         printk(KERN_ALERT "Error creating procfs entry!\n");
  57.         error_level=1;
  58.         return -1;
  59.     }
  60.     sock = netlink_kernel_create(&init_net,NETLINK_TEST, &netlink_configuration);
  61.     if(!sock) {
  62.         printk(KERN_ALERT "Error creating netlink socket!\n");
  63.         error_level=2;
  64.         return -2;
  65.     }
  66.     return 0;
  67. }
  68.  
  69. static void __exit netlink_stop(void)
  70. {
  71.     if(error_level!=1)
  72.         remove_proc_entry("netlink",NULL);
  73.     if(error_level!=2)
  74.         netlink_kernel_release(sock);
  75. }
  76.  
  77. module_init(netlink_start);
  78. module_exit(netlink_stop);
  79. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement