James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 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> |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 24 | #include <linux/delay.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Paul Gortmaker | 0970366 | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 27 | #include <net/sock.h> |
| 28 | #include <net/netlink.h> |
| 29 | |
| 30 | #include <scsi/scsi_netlink.h> |
| 31 | #include "scsi_priv.h" |
| 32 | |
| 33 | struct sock *scsi_nl_sock = NULL; |
| 34 | EXPORT_SYMBOL_GPL(scsi_nl_sock); |
| 35 | |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 36 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 37 | * scsi_nl_rcv_msg - Receive message handler. |
| 38 | * @skb: socket receive buffer |
| 39 | * |
| 40 | * Description: Extracts message from a receive buffer. |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 41 | * Validates message header and calls appropriate transport message handler |
| 42 | * |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 43 | * |
| 44 | **/ |
| 45 | static void |
| 46 | scsi_nl_rcv_msg(struct sk_buff *skb) |
| 47 | { |
| 48 | struct nlmsghdr *nlh; |
| 49 | struct scsi_nl_hdr *hdr; |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 50 | u32 rlen; |
| 51 | int err, tport; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 52 | |
Hong zhi guo | e07ebea | 2013-03-27 06:53:15 +0000 | [diff] [blame] | 53 | while (skb->len >= NLMSG_HDRLEN) { |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 54 | err = 0; |
| 55 | |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 56 | nlh = nlmsg_hdr(skb); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 57 | if ((nlh->nlmsg_len < (sizeof(*nlh) + sizeof(*hdr))) || |
| 58 | (skb->len < nlh->nlmsg_len)) { |
| 59 | printk(KERN_WARNING "%s: discarding partial skb\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 60 | __func__); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 65 | if (rlen > skb->len) |
| 66 | rlen = skb->len; |
| 67 | |
| 68 | if (nlh->nlmsg_type != SCSI_TRANSPORT_MSG) { |
| 69 | err = -EBADMSG; |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 70 | goto next_msg; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Hong zhi guo | e07ebea | 2013-03-27 06:53:15 +0000 | [diff] [blame] | 73 | hdr = nlmsg_data(nlh); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 74 | if ((hdr->version != SCSI_NL_VERSION) || |
| 75 | (hdr->magic != SCSI_NL_MAGIC)) { |
| 76 | err = -EPROTOTYPE; |
| 77 | goto next_msg; |
| 78 | } |
| 79 | |
Eric W. Biederman | 90f62cf | 2014-04-23 14:29:27 -0700 | [diff] [blame] | 80 | if (!netlink_capable(skb, CAP_SYS_ADMIN)) { |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 81 | err = -EPERM; |
| 82 | goto next_msg; |
| 83 | } |
| 84 | |
| 85 | if (nlh->nlmsg_len < (sizeof(*nlh) + hdr->msglen)) { |
| 86 | printk(KERN_WARNING "%s: discarding partial message\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 87 | __func__); |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 88 | goto next_msg; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 92 | * Deliver message to the appropriate transport |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 93 | */ |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 94 | tport = hdr->transport; |
Eric W. Biederman | 8289bab | 2012-09-07 12:39:21 +0000 | [diff] [blame] | 95 | if (tport == SCSI_NL_TRANSPORT) { |
| 96 | switch (hdr->msgtype) { |
| 97 | case SCSI_NL_SHOST_VENDOR: |
| 98 | /* Locate the driver that corresponds to the message */ |
| 99 | err = -ESRCH; |
| 100 | break; |
| 101 | default: |
| 102 | err = -EBADR; |
| 103 | break; |
| 104 | } |
| 105 | if (err) |
| 106 | printk(KERN_WARNING "%s: Msgtype %d failed - err %d\n", |
| 107 | __func__, hdr->msgtype, err); |
| 108 | } |
| 109 | else |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 110 | err = -ENOENT; |
| 111 | |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 112 | next_msg: |
| 113 | if ((err) || (nlh->nlmsg_flags & NLM_F_ACK)) |
Johannes Berg | 2d4bc93 | 2017-04-12 14:34:04 +0200 | [diff] [blame] | 114 | netlink_ack(skb, nlh, err, NULL); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 115 | |
| 116 | skb_pull(skb, rlen); |
| 117 | } |
| 118 | } |
| 119 | |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 120 | /** |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 121 | * scsi_netlink_init - Called by SCSI subsystem to initialize |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 122 | * the SCSI transport netlink interface |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 123 | * |
| 124 | **/ |
| 125 | void |
| 126 | scsi_netlink_init(void) |
| 127 | { |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 128 | struct netlink_kernel_cfg cfg = { |
| 129 | .input = scsi_nl_rcv_msg, |
| 130 | .groups = SCSI_NL_GRP_CNT, |
| 131 | }; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 132 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 133 | scsi_nl_sock = netlink_kernel_create(&init_net, NETLINK_SCSITRANSPORT, |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 134 | &cfg); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 135 | if (!scsi_nl_sock) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 136 | printk(KERN_ERR "%s: register of receive handler failed\n", |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 137 | __func__); |
James Smart | 22447be | 2008-08-08 02:14:18 -0400 | [diff] [blame] | 138 | return; |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
Rob Landley | eb44820 | 2007-11-03 13:30:39 -0500 | [diff] [blame] | 146 | * 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] | 147 | * |
| 148 | **/ |
| 149 | void |
| 150 | scsi_netlink_exit(void) |
| 151 | { |
| 152 | if (scsi_nl_sock) { |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 153 | netlink_kernel_release(scsi_nl_sock); |
James Smart | 84314fd | 2006-08-18 17:30:09 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return; |
| 157 | } |
| 158 | |