scsi_netlink.c 2.9 KB

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