scsi_netlink.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. goto next_msg;
  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_msg -
  87. * Receive handler for a socket. Extracts a received message buffer from
  88. * the socket, and starts message processing.
  89. *
  90. * @sk: socket
  91. * @len: unused
  92. *
  93. **/
  94. static void
  95. scsi_nl_rcv(struct sock *sk, int len)
  96. {
  97. struct sk_buff *skb;
  98. while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
  99. scsi_nl_rcv_msg(skb);
  100. kfree_skb(skb);
  101. }
  102. }
  103. /**
  104. * scsi_nl_rcv_event -
  105. * Event handler for a netlink socket.
  106. *
  107. * @this: event notifier block
  108. * @event: event type
  109. * @ptr: event payload
  110. *
  111. **/
  112. static int
  113. scsi_nl_rcv_event(struct notifier_block *this, unsigned long event, void *ptr)
  114. {
  115. struct netlink_notify *n = ptr;
  116. if (n->protocol != NETLINK_SCSITRANSPORT)
  117. return NOTIFY_DONE;
  118. /*
  119. * Currently, we are not tracking PID's, etc. There is nothing
  120. * to handle.
  121. */
  122. return NOTIFY_DONE;
  123. }
  124. static struct notifier_block scsi_netlink_notifier = {
  125. .notifier_call = scsi_nl_rcv_event,
  126. };
  127. /**
  128. * scsi_netlink_init -
  129. * Called by SCSI subsystem to intialize the SCSI transport netlink
  130. * interface
  131. *
  132. **/
  133. void
  134. scsi_netlink_init(void)
  135. {
  136. int error;
  137. error = netlink_register_notifier(&scsi_netlink_notifier);
  138. if (error) {
  139. printk(KERN_ERR "%s: register of event handler failed - %d\n",
  140. __FUNCTION__, error);
  141. return;
  142. }
  143. scsi_nl_sock = netlink_kernel_create(NETLINK_SCSITRANSPORT,
  144. SCSI_NL_GRP_CNT, scsi_nl_rcv, NULL,
  145. THIS_MODULE);
  146. if (!scsi_nl_sock) {
  147. printk(KERN_ERR "%s: register of recieve handler failed\n",
  148. __FUNCTION__);
  149. netlink_unregister_notifier(&scsi_netlink_notifier);
  150. }
  151. return;
  152. }
  153. /**
  154. * scsi_netlink_exit -
  155. * Called by SCSI subsystem to disable the SCSI transport netlink
  156. * interface
  157. *
  158. **/
  159. void
  160. scsi_netlink_exit(void)
  161. {
  162. if (scsi_nl_sock) {
  163. sock_release(scsi_nl_sock->sk_socket);
  164. netlink_unregister_notifier(&scsi_netlink_notifier);
  165. }
  166. return;
  167. }