1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * scsi_netlink.c - SCSI Transport Netlink Interface
5 * Copyright (C) 2006 James Smart, Emulex Corporation
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>
14 #include <net/netlink.h>
16 #include <scsi/scsi_netlink.h>
17 #include "scsi_priv.h"
19 struct sock
*scsi_nl_sock
= NULL
;
20 EXPORT_SYMBOL_GPL(scsi_nl_sock
);
23 * scsi_nl_rcv_msg - Receive message handler.
24 * @skb: socket receive buffer
26 * Description: Extracts message from a receive buffer.
27 * Validates message header and calls appropriate transport message handler
32 scsi_nl_rcv_msg(struct sk_buff
*skb
)
35 struct scsi_nl_hdr
*hdr
;
39 while (skb
->len
>= NLMSG_HDRLEN
) {
43 if ((nlh
->nlmsg_len
< (sizeof(*nlh
) + sizeof(*hdr
))) ||
44 (skb
->len
< nlh
->nlmsg_len
)) {
45 printk(KERN_WARNING
"%s: discarding partial skb\n",
50 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
54 if (nlh
->nlmsg_type
!= SCSI_TRANSPORT_MSG
) {
59 hdr
= nlmsg_data(nlh
);
60 if ((hdr
->version
!= SCSI_NL_VERSION
) ||
61 (hdr
->magic
!= SCSI_NL_MAGIC
)) {
66 if (!netlink_capable(skb
, CAP_SYS_ADMIN
)) {
71 if (nlh
->nlmsg_len
< (sizeof(*nlh
) + hdr
->msglen
)) {
72 printk(KERN_WARNING
"%s: discarding partial message\n",
78 * Deliver message to the appropriate transport
80 tport
= hdr
->transport
;
81 if (tport
== SCSI_NL_TRANSPORT
) {
82 switch (hdr
->msgtype
) {
83 case SCSI_NL_SHOST_VENDOR
:
84 /* Locate the driver that corresponds to the message */
92 printk(KERN_WARNING
"%s: Msgtype %d failed - err %d\n",
93 __func__
, hdr
->msgtype
, err
);
99 if ((err
) || (nlh
->nlmsg_flags
& NLM_F_ACK
))
100 netlink_ack(skb
, nlh
, err
, NULL
);
107 * scsi_netlink_init - Called by SCSI subsystem to initialize
108 * the SCSI transport netlink interface
112 scsi_netlink_init(void)
114 struct netlink_kernel_cfg cfg
= {
115 .input
= scsi_nl_rcv_msg
,
116 .groups
= SCSI_NL_GRP_CNT
,
119 scsi_nl_sock
= netlink_kernel_create(&init_net
, NETLINK_SCSITRANSPORT
,
122 printk(KERN_ERR
"%s: register of receive handler failed\n",
132 * scsi_netlink_exit - Called by SCSI subsystem to disable the SCSI transport netlink interface
136 scsi_netlink_exit(void)
139 netlink_kernel_release(scsi_nl_sock
);