scsi_netlink.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * scsi_netlink.c - SCSI Transport Netlink Interface
  3. *
  4. * Copyright (C) 2006 James Smart, Emulex Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/time.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/security.h>
  24. #include <net/sock.h>
  25. #include <net/netlink.h>
  26. #include <scsi/scsi_netlink.h>
  27. #include "scsi_priv.h"
  28. struct sock *scsi_nl_sock = NULL;
  29. EXPORT_SYMBOL_GPL(scsi_nl_sock);
  30. /**
  31. * scsi_nl_rcv_msg - Receive message handler.
  32. * @skb: socket receive buffer
  33. *
  34. * Description: Extracts message from a receive buffer.
  35. * Validates message header and calls appropriate transport message handler
  36. *
  37. *
  38. **/
  39. static void
  40. scsi_nl_rcv_msg(struct sk_buff *skb)
  41. {
  42. struct nlmsghdr *nlh;
  43. struct scsi_nl_hdr *hdr;
  44. uint32_t rlen;
  45. int err;
  46. while (skb->len >= NLMSG_SPACE(0)) {
  47. err = 0;
  48. nlh = nlmsg_hdr(skb);
  49. if ((nlh->nlmsg_len < (sizeof(*nlh) + sizeof(*hdr))) ||
  50. (skb->len < nlh->nlmsg_len)) {
  51. printk(KERN_WARNING "%s: discarding partial skb\n",
  52. __FUNCTION__);
  53. return;
  54. }
  55. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  56. if (rlen > skb->len)
  57. rlen = skb->len;
  58. if (nlh->nlmsg_type != SCSI_TRANSPORT_MSG) {
  59. err = -EBADMSG;
  60. return;
  61. }
  62. hdr = NLMSG_DATA(nlh);
  63. if ((hdr->version != SCSI_NL_VERSION) ||
  64. (hdr->magic != SCSI_NL_MAGIC)) {
  65. err = -EPROTOTYPE;
  66. goto next_msg;
  67. }
  68. if (security_netlink_recv(skb, CAP_SYS_ADMIN)) {
  69. err = -EPERM;
  70. goto next_msg;
  71. }
  72. if (nlh->nlmsg_len < (sizeof(*nlh) + hdr->msglen)) {
  73. printk(KERN_WARNING "%s: discarding partial message\n",
  74. __FUNCTION__);
  75. return;
  76. }
  77. /*
  78. * We currently don't support anyone sending us a message
  79. */
  80. next_msg:
  81. if ((err) || (nlh->nlmsg_flags & NLM_F_ACK))
  82. netlink_ack(skb, nlh, err);
  83. skb_pull(skb, rlen);
  84. }
  85. }
  86. /**
  87. * scsi_nl_rcv_event - Event handler for a netlink socket.
  88. * @this: event notifier block
  89. * @event: event type
  90. * @ptr: event payload
  91. *
  92. **/
  93. static int
  94. scsi_nl_rcv_event(struct notifier_block *this, unsigned long event, void *ptr)
  95. {
  96. struct netlink_notify *n = ptr;
  97. if (n->protocol != NETLINK_SCSITRANSPORT)
  98. return NOTIFY_DONE;
  99. /*
  100. * Currently, we are not tracking PID's, etc. There is nothing
  101. * to handle.
  102. */
  103. return NOTIFY_DONE;
  104. }
  105. static struct notifier_block scsi_netlink_notifier = {
  106. .notifier_call = scsi_nl_rcv_event,
  107. };
  108. /**
  109. * scsi_netlink_init - Called by SCSI subsystem to intialize the SCSI transport netlink interface
  110. *
  111. **/
  112. void
  113. scsi_netlink_init(void)
  114. {
  115. int error;
  116. error = netlink_register_notifier(&scsi_netlink_notifier);
  117. if (error) {
  118. printk(KERN_ERR "%s: register of event handler failed - %d\n",
  119. __FUNCTION__, error);
  120. return;
  121. }
  122. scsi_nl_sock = netlink_kernel_create(&init_net, NETLINK_SCSITRANSPORT,
  123. SCSI_NL_GRP_CNT, scsi_nl_rcv_msg, NULL,
  124. THIS_MODULE);
  125. if (!scsi_nl_sock) {
  126. printk(KERN_ERR "%s: register of recieve handler failed\n",
  127. __FUNCTION__);
  128. netlink_unregister_notifier(&scsi_netlink_notifier);
  129. }
  130. return;
  131. }
  132. /**
  133. * scsi_netlink_exit - Called by SCSI subsystem to disable the SCSI transport netlink interface
  134. *
  135. **/
  136. void
  137. scsi_netlink_exit(void)
  138. {
  139. if (scsi_nl_sock) {
  140. sock_release(scsi_nl_sock->sk_socket);
  141. netlink_unregister_notifier(&scsi_netlink_notifier);
  142. }
  143. return;
  144. }