scsi_netlink.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/export.h>
  27. #include <net/sock.h>
  28. #include <net/netlink.h>
  29. #include <scsi/scsi_netlink.h>
  30. #include "scsi_priv.h"
  31. struct sock *scsi_nl_sock = NULL;
  32. EXPORT_SYMBOL_GPL(scsi_nl_sock);
  33. /**
  34. * scsi_nl_rcv_msg - Receive message handler.
  35. * @skb: socket receive buffer
  36. *
  37. * Description: Extracts message from a receive buffer.
  38. * Validates message header and calls appropriate transport message handler
  39. *
  40. *
  41. **/
  42. static void
  43. scsi_nl_rcv_msg(struct sk_buff *skb)
  44. {
  45. struct nlmsghdr *nlh;
  46. struct scsi_nl_hdr *hdr;
  47. u32 rlen;
  48. int err, tport;
  49. while (skb->len >= NLMSG_HDRLEN) {
  50. err = 0;
  51. nlh = nlmsg_hdr(skb);
  52. if ((nlh->nlmsg_len < (sizeof(*nlh) + sizeof(*hdr))) ||
  53. (skb->len < nlh->nlmsg_len)) {
  54. printk(KERN_WARNING "%s: discarding partial skb\n",
  55. __func__);
  56. return;
  57. }
  58. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  59. if (rlen > skb->len)
  60. rlen = skb->len;
  61. if (nlh->nlmsg_type != SCSI_TRANSPORT_MSG) {
  62. err = -EBADMSG;
  63. goto next_msg;
  64. }
  65. hdr = nlmsg_data(nlh);
  66. if ((hdr->version != SCSI_NL_VERSION) ||
  67. (hdr->magic != SCSI_NL_MAGIC)) {
  68. err = -EPROTOTYPE;
  69. goto next_msg;
  70. }
  71. if (!netlink_capable(skb, CAP_SYS_ADMIN)) {
  72. err = -EPERM;
  73. goto next_msg;
  74. }
  75. if (nlh->nlmsg_len < (sizeof(*nlh) + hdr->msglen)) {
  76. printk(KERN_WARNING "%s: discarding partial message\n",
  77. __func__);
  78. goto next_msg;
  79. }
  80. /*
  81. * Deliver message to the appropriate transport
  82. */
  83. tport = hdr->transport;
  84. if (tport == SCSI_NL_TRANSPORT) {
  85. switch (hdr->msgtype) {
  86. case SCSI_NL_SHOST_VENDOR:
  87. /* Locate the driver that corresponds to the message */
  88. err = -ESRCH;
  89. break;
  90. default:
  91. err = -EBADR;
  92. break;
  93. }
  94. if (err)
  95. printk(KERN_WARNING "%s: Msgtype %d failed - err %d\n",
  96. __func__, hdr->msgtype, err);
  97. }
  98. else
  99. err = -ENOENT;
  100. next_msg:
  101. if ((err) || (nlh->nlmsg_flags & NLM_F_ACK))
  102. netlink_ack(skb, nlh, err);
  103. skb_pull(skb, rlen);
  104. }
  105. }
  106. /**
  107. * scsi_netlink_init - Called by SCSI subsystem to initialize
  108. * the SCSI transport netlink interface
  109. *
  110. **/
  111. void
  112. scsi_netlink_init(void)
  113. {
  114. struct netlink_kernel_cfg cfg = {
  115. .input = scsi_nl_rcv_msg,
  116. .groups = SCSI_NL_GRP_CNT,
  117. };
  118. scsi_nl_sock = netlink_kernel_create(&init_net, NETLINK_SCSITRANSPORT,
  119. &cfg);
  120. if (!scsi_nl_sock) {
  121. printk(KERN_ERR "%s: register of receive handler failed\n",
  122. __func__);
  123. return;
  124. }
  125. return;
  126. }
  127. /**
  128. * scsi_netlink_exit - Called by SCSI subsystem to disable the SCSI transport netlink interface
  129. *
  130. **/
  131. void
  132. scsi_netlink_exit(void)
  133. {
  134. if (scsi_nl_sock) {
  135. netlink_kernel_release(scsi_nl_sock);
  136. }
  137. return;
  138. }