scsi_netlink.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 -
  32. * Receive message handler. Extracts message from a receive buffer.
  33. * Validates message header and calls appropriate transport message handler
  34. *
  35. * @skb: socket receive buffer
  36. *
  37. **/
  38. static void
  39. scsi_nl_rcv_msg(struct sk_buff *skb)
  40. {
  41. struct nlmsghdr *nlh;
  42. struct scsi_nl_hdr *hdr;
  43. uint32_t rlen;
  44. int err;
  45. while (skb->len >= NLMSG_SPACE(0)) {
  46. err = 0;
  47. nlh = nlmsg_hdr(skb);
  48. if ((nlh->nlmsg_len < (sizeof(*nlh) + sizeof(*hdr))) ||
  49. (skb->len < nlh->nlmsg_len)) {
  50. printk(KERN_WARNING "%s: discarding partial skb\n",
  51. __FUNCTION__);
  52. return;
  53. }
  54. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  55. if (rlen > skb->len)
  56. rlen = skb->len;
  57. if (nlh->nlmsg_type != SCSI_TRANSPORT_MSG) {
  58. err = -EBADMSG;
  59. return;
  60. }
  61. hdr = NLMSG_DATA(nlh);
  62. if ((hdr->version != SCSI_NL_VERSION) ||
  63. (hdr->magic != SCSI_NL_MAGIC)) {
  64. err = -EPROTOTYPE;
  65. goto next_msg;
  66. }
  67. if (security_netlink_recv(skb, CAP_SYS_ADMIN)) {
  68. err = -EPERM;
  69. goto next_msg;
  70. }
  71. if (nlh->nlmsg_len < (sizeof(*nlh) + hdr->msglen)) {
  72. printk(KERN_WARNING "%s: discarding partial message\n",
  73. __FUNCTION__);
  74. return;
  75. }
  76. /*
  77. * We currently don't support anyone sending us a message
  78. */
  79. next_msg:
  80. if ((err) || (nlh->nlmsg_flags & NLM_F_ACK))
  81. netlink_ack(skb, nlh, err);
  82. skb_pull(skb, rlen);
  83. }
  84. }
  85. /**
  86. * scsi_nl_rcv_event -
  87. * Event handler for a netlink socket.
  88. *
  89. * @this: event notifier block
  90. * @event: event type
  91. * @ptr: event payload
  92. *
  93. **/
  94. static int
  95. scsi_nl_rcv_event(struct notifier_block *this, unsigned long event, void *ptr)
  96. {
  97. struct netlink_notify *n = ptr;
  98. if (n->protocol != NETLINK_SCSITRANSPORT)
  99. return NOTIFY_DONE;
  100. /*
  101. * Currently, we are not tracking PID's, etc. There is nothing
  102. * to handle.
  103. */
  104. return NOTIFY_DONE;
  105. }
  106. static struct notifier_block scsi_netlink_notifier = {
  107. .notifier_call = scsi_nl_rcv_event,
  108. };
  109. /**
  110. * scsi_netlink_init -
  111. * Called by SCSI subsystem to intialize the SCSI transport netlink
  112. * interface
  113. *
  114. **/
  115. void
  116. scsi_netlink_init(void)
  117. {
  118. int error;
  119. error = netlink_register_notifier(&scsi_netlink_notifier);
  120. if (error) {
  121. printk(KERN_ERR "%s: register of event handler failed - %d\n",
  122. __FUNCTION__, error);
  123. return;
  124. }
  125. scsi_nl_sock = netlink_kernel_create(&init_net, NETLINK_SCSITRANSPORT,
  126. SCSI_NL_GRP_CNT, scsi_nl_rcv_msg, NULL,
  127. THIS_MODULE);
  128. if (!scsi_nl_sock) {
  129. printk(KERN_ERR "%s: register of recieve handler failed\n",
  130. __FUNCTION__);
  131. netlink_unregister_notifier(&scsi_netlink_notifier);
  132. }
  133. return;
  134. }
  135. /**
  136. * scsi_netlink_exit -
  137. * Called by SCSI subsystem to disable the SCSI transport netlink
  138. * interface
  139. *
  140. **/
  141. void
  142. scsi_netlink_exit(void)
  143. {
  144. if (scsi_nl_sock) {
  145. sock_release(scsi_nl_sock->sk_socket);
  146. netlink_unregister_notifier(&scsi_netlink_notifier);
  147. }
  148. return;
  149. }