blob: d7f76fd84256afbf2a954bf68ab5b7bdb97e87d3 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
James Smart84314fd2006-08-18 17:30:09 -04002/*
3 * scsi_netlink.c - SCSI Transport Netlink Interface
4 *
5 * Copyright (C) 2006 James Smart, Emulex Corporation
James Smart84314fd2006-08-18 17:30:09 -04006 */
7#include <linux/time.h>
8#include <linux/jiffies.h>
9#include <linux/security.h>
James Smart22447be2008-08-08 02:14:18 -040010#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040012#include <linux/export.h>
James Smart84314fd2006-08-18 17:30:09 -040013#include <net/sock.h>
14#include <net/netlink.h>
15
16#include <scsi/scsi_netlink.h>
17#include "scsi_priv.h"
18
19struct sock *scsi_nl_sock = NULL;
20EXPORT_SYMBOL_GPL(scsi_nl_sock);
21
James Smart84314fd2006-08-18 17:30:09 -040022/**
Rob Landleyeb448202007-11-03 13:30:39 -050023 * scsi_nl_rcv_msg - Receive message handler.
24 * @skb: socket receive buffer
25 *
26 * Description: Extracts message from a receive buffer.
James Smart84314fd2006-08-18 17:30:09 -040027 * Validates message header and calls appropriate transport message handler
28 *
James Smart84314fd2006-08-18 17:30:09 -040029 *
30 **/
31static void
32scsi_nl_rcv_msg(struct sk_buff *skb)
33{
34 struct nlmsghdr *nlh;
35 struct scsi_nl_hdr *hdr;
James Smart22447be2008-08-08 02:14:18 -040036 u32 rlen;
37 int err, tport;
James Smart84314fd2006-08-18 17:30:09 -040038
Hong zhi guoe07ebea2013-03-27 06:53:15 +000039 while (skb->len >= NLMSG_HDRLEN) {
James Smart84314fd2006-08-18 17:30:09 -040040 err = 0;
41
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -070042 nlh = nlmsg_hdr(skb);
James Smart84314fd2006-08-18 17:30:09 -040043 if ((nlh->nlmsg_len < (sizeof(*nlh) + sizeof(*hdr))) ||
44 (skb->len < nlh->nlmsg_len)) {
45 printk(KERN_WARNING "%s: discarding partial skb\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070046 __func__);
James Smart84314fd2006-08-18 17:30:09 -040047 return;
48 }
49
50 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
51 if (rlen > skb->len)
52 rlen = skb->len;
53
54 if (nlh->nlmsg_type != SCSI_TRANSPORT_MSG) {
55 err = -EBADMSG;
James Smart22447be2008-08-08 02:14:18 -040056 goto next_msg;
James Smart84314fd2006-08-18 17:30:09 -040057 }
58
Hong zhi guoe07ebea2013-03-27 06:53:15 +000059 hdr = nlmsg_data(nlh);
James Smart84314fd2006-08-18 17:30:09 -040060 if ((hdr->version != SCSI_NL_VERSION) ||
61 (hdr->magic != SCSI_NL_MAGIC)) {
62 err = -EPROTOTYPE;
63 goto next_msg;
64 }
65
Eric W. Biederman90f62cf2014-04-23 14:29:27 -070066 if (!netlink_capable(skb, CAP_SYS_ADMIN)) {
James Smart84314fd2006-08-18 17:30:09 -040067 err = -EPERM;
68 goto next_msg;
69 }
70
71 if (nlh->nlmsg_len < (sizeof(*nlh) + hdr->msglen)) {
72 printk(KERN_WARNING "%s: discarding partial message\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -070073 __func__);
James Smart22447be2008-08-08 02:14:18 -040074 goto next_msg;
James Smart84314fd2006-08-18 17:30:09 -040075 }
76
77 /*
James Smart22447be2008-08-08 02:14:18 -040078 * Deliver message to the appropriate transport
James Smart84314fd2006-08-18 17:30:09 -040079 */
James Smart22447be2008-08-08 02:14:18 -040080 tport = hdr->transport;
Eric W. Biederman8289bab2012-09-07 12:39:21 +000081 if (tport == SCSI_NL_TRANSPORT) {
82 switch (hdr->msgtype) {
83 case SCSI_NL_SHOST_VENDOR:
84 /* Locate the driver that corresponds to the message */
85 err = -ESRCH;
86 break;
87 default:
88 err = -EBADR;
89 break;
90 }
91 if (err)
92 printk(KERN_WARNING "%s: Msgtype %d failed - err %d\n",
93 __func__, hdr->msgtype, err);
94 }
95 else
James Smart22447be2008-08-08 02:14:18 -040096 err = -ENOENT;
97
James Smart84314fd2006-08-18 17:30:09 -040098next_msg:
99 if ((err) || (nlh->nlmsg_flags & NLM_F_ACK))
Johannes Berg2d4bc932017-04-12 14:34:04 +0200100 netlink_ack(skb, nlh, err, NULL);
James Smart84314fd2006-08-18 17:30:09 -0400101
102 skb_pull(skb, rlen);
103 }
104}
105
James Smart22447be2008-08-08 02:14:18 -0400106/**
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400107 * scsi_netlink_init - Called by SCSI subsystem to initialize
James Smart22447be2008-08-08 02:14:18 -0400108 * the SCSI transport netlink interface
James Smart84314fd2006-08-18 17:30:09 -0400109 *
110 **/
111void
112scsi_netlink_init(void)
113{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000114 struct netlink_kernel_cfg cfg = {
115 .input = scsi_nl_rcv_msg,
116 .groups = SCSI_NL_GRP_CNT,
117 };
James Smart84314fd2006-08-18 17:30:09 -0400118
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200119 scsi_nl_sock = netlink_kernel_create(&init_net, NETLINK_SCSITRANSPORT,
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000120 &cfg);
James Smart84314fd2006-08-18 17:30:09 -0400121 if (!scsi_nl_sock) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300122 printk(KERN_ERR "%s: register of receive handler failed\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700123 __func__);
James Smart22447be2008-08-08 02:14:18 -0400124 return;
James Smart84314fd2006-08-18 17:30:09 -0400125 }
126
127 return;
128}
129
130
131/**
Rob Landleyeb448202007-11-03 13:30:39 -0500132 * scsi_netlink_exit - Called by SCSI subsystem to disable the SCSI transport netlink interface
James Smart84314fd2006-08-18 17:30:09 -0400133 *
134 **/
135void
136scsi_netlink_exit(void)
137{
138 if (scsi_nl_sock) {
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800139 netlink_kernel_release(scsi_nl_sock);
James Smart84314fd2006-08-18 17:30:09 -0400140 }
141
142 return;
143}
144