Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 2 | /* |
| 3 | * scsi_netlink.c - SCSI Transport Netlink Interface |
| 4 | * |
| 5 | * Copyright (C) 2006 James Smart, Emulex Corporation |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 6 | */ |
| 7 | #include <linux/time.h> |
| 8 | #include <linux/jiffies.h> |
| 9 | #include <linux/security.h> |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 10 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Paul Gortmaker | 0970366 | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 13 | #include <net/sock.h> |
| 14 | #include <net/netlink.h> |
| 15 | |
| 16 | #include <scsi/scsi_netlink.h> |
| 17 | #include "scsi_priv.h" |
| 18 | |
| 19 | struct sock *scsi_nl_sock = NULL; |
| 20 | EXPORT_SYMBOL_GPL(scsi_nl_sock); |
| 21 | |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 22 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 23 | * scsi_nl_rcv_msg - Receive message handler. |
| 24 | * @skb: socket receive buffer |
| 25 | * |
| 26 | * Description: Extracts message from a receive buffer. |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 27 | * Validates message header and calls appropriate transport message handler |
| 28 | * |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 29 | * |
| 30 | **/ |
| 31 | static void |
| 32 | scsi_nl_rcv_msg(struct sk_buff *skb) |
| 33 | { |
| 34 | struct nlmsghdr *nlh; |
| 35 | struct scsi_nl_hdr *hdr; |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 36 | u32 rlen; |
| 37 | int err, tport; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 38 | |
Hong zhi guo | e07ebea | 2013-03-27 06:53:15 +0000 | [diff] [blame] | 39 | while (skb->len >= NLMSG_HDRLEN) { |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 40 | err = 0; |
| 41 | |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 42 | nlh = nlmsg_hdr(skb); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 43 | if ((nlh->nlmsg_len < (sizeof(*nlh) + sizeof(*hdr))) || |
| 44 | (skb->len < nlh->nlmsg_len)) { |
| 45 | printk(KERN_WARNING "%s: discarding partial skb\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 46 | __func__); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 47 | 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 Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 56 | goto next_msg; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Hong zhi guo | e07ebea | 2013-03-27 06:53:15 +0000 | [diff] [blame] | 59 | hdr = nlmsg_data(nlh); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 60 | if ((hdr->version != SCSI_NL_VERSION) || |
| 61 | (hdr->magic != SCSI_NL_MAGIC)) { |
| 62 | err = -EPROTOTYPE; |
| 63 | goto next_msg; |
| 64 | } |
| 65 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 66 | if (!netlink_capable(skb, CAP_SYS_ADMIN)) { |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 67 | 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 Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 73 | __func__); |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 74 | goto next_msg; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 78 | * Deliver message to the appropriate transport |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 79 | */ |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 80 | tport = hdr->transport; |
Eric W. Biederman | 8289bab | 2012-09-07 12:39:21 +0000 | [diff] [blame] | 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 */ |
| 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 Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 96 | err = -ENOENT; |
| 97 | |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 98 | next_msg: |
| 99 | if ((err) || (nlh->nlmsg_flags & NLM_F_ACK)) |
Johannes Berg | 2d4bc93 | 2017-04-12 14:34:04 +0200 | [diff] [blame] | 100 | netlink_ack(skb, nlh, err, NULL); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 101 | |
| 102 | skb_pull(skb, rlen); |
| 103 | } |
| 104 | } |
| 105 | |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 106 | /** |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 107 | * scsi_netlink_init - Called by SCSI subsystem to initialize |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 108 | * the SCSI transport netlink interface |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 109 | * |
| 110 | **/ |
| 111 | void |
| 112 | scsi_netlink_init(void) |
| 113 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 114 | struct netlink_kernel_cfg cfg = { |
| 115 | .input = scsi_nl_rcv_msg, |
| 116 | .groups = SCSI_NL_GRP_CNT, |
| 117 | }; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 118 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 119 | scsi_nl_sock = netlink_kernel_create(&init_net, NETLINK_SCSITRANSPORT, |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 120 | &cfg); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 121 | if (!scsi_nl_sock) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 122 | printk(KERN_ERR "%s: register of receive handler failed\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 123 | __func__); |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 124 | return; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 132 | * scsi_netlink_exit - Called by SCSI subsystem to disable the SCSI transport netlink interface |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 133 | * |
| 134 | **/ |
| 135 | void |
| 136 | scsi_netlink_exit(void) |
| 137 | { |
| 138 | if (scsi_nl_sock) { |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 139 | netlink_kernel_release(scsi_nl_sock); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return; |
| 143 | } |
| 144 | |