18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * scsi_netlink.c - SCSI Transport Netlink Interface 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2006 James Smart, Emulex Corporation 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci#include <linux/time.h> 88c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 98c2ecf20Sopenharmony_ci#include <linux/security.h> 108c2ecf20Sopenharmony_ci#include <linux/delay.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include <linux/export.h> 138c2ecf20Sopenharmony_ci#include <net/sock.h> 148c2ecf20Sopenharmony_ci#include <net/netlink.h> 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#include <scsi/scsi_netlink.h> 178c2ecf20Sopenharmony_ci#include "scsi_priv.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistruct sock *scsi_nl_sock = NULL; 208c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(scsi_nl_sock); 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/** 238c2ecf20Sopenharmony_ci * scsi_nl_rcv_msg - Receive message handler. 248c2ecf20Sopenharmony_ci * @skb: socket receive buffer 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * Description: Extracts message from a receive buffer. 278c2ecf20Sopenharmony_ci * Validates message header and calls appropriate transport message handler 288c2ecf20Sopenharmony_ci * 298c2ecf20Sopenharmony_ci * 308c2ecf20Sopenharmony_ci **/ 318c2ecf20Sopenharmony_cistatic void 328c2ecf20Sopenharmony_ciscsi_nl_rcv_msg(struct sk_buff *skb) 338c2ecf20Sopenharmony_ci{ 348c2ecf20Sopenharmony_ci struct nlmsghdr *nlh; 358c2ecf20Sopenharmony_ci struct scsi_nl_hdr *hdr; 368c2ecf20Sopenharmony_ci u32 rlen; 378c2ecf20Sopenharmony_ci int err, tport; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci while (skb->len >= NLMSG_HDRLEN) { 408c2ecf20Sopenharmony_ci err = 0; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci nlh = nlmsg_hdr(skb); 438c2ecf20Sopenharmony_ci if ((nlh->nlmsg_len < (sizeof(*nlh) + sizeof(*hdr))) || 448c2ecf20Sopenharmony_ci (skb->len < nlh->nlmsg_len)) { 458c2ecf20Sopenharmony_ci printk(KERN_WARNING "%s: discarding partial skb\n", 468c2ecf20Sopenharmony_ci __func__); 478c2ecf20Sopenharmony_ci return; 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci rlen = NLMSG_ALIGN(nlh->nlmsg_len); 518c2ecf20Sopenharmony_ci if (rlen > skb->len) 528c2ecf20Sopenharmony_ci rlen = skb->len; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci if (nlh->nlmsg_type != SCSI_TRANSPORT_MSG) { 558c2ecf20Sopenharmony_ci err = -EBADMSG; 568c2ecf20Sopenharmony_ci goto next_msg; 578c2ecf20Sopenharmony_ci } 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci hdr = nlmsg_data(nlh); 608c2ecf20Sopenharmony_ci if ((hdr->version != SCSI_NL_VERSION) || 618c2ecf20Sopenharmony_ci (hdr->magic != SCSI_NL_MAGIC)) { 628c2ecf20Sopenharmony_ci err = -EPROTOTYPE; 638c2ecf20Sopenharmony_ci goto next_msg; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci if (!netlink_capable(skb, CAP_SYS_ADMIN)) { 678c2ecf20Sopenharmony_ci err = -EPERM; 688c2ecf20Sopenharmony_ci goto next_msg; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci if (nlh->nlmsg_len < (sizeof(*nlh) + hdr->msglen)) { 728c2ecf20Sopenharmony_ci printk(KERN_WARNING "%s: discarding partial message\n", 738c2ecf20Sopenharmony_ci __func__); 748c2ecf20Sopenharmony_ci goto next_msg; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci /* 788c2ecf20Sopenharmony_ci * Deliver message to the appropriate transport 798c2ecf20Sopenharmony_ci */ 808c2ecf20Sopenharmony_ci tport = hdr->transport; 818c2ecf20Sopenharmony_ci if (tport == SCSI_NL_TRANSPORT) { 828c2ecf20Sopenharmony_ci switch (hdr->msgtype) { 838c2ecf20Sopenharmony_ci case SCSI_NL_SHOST_VENDOR: 848c2ecf20Sopenharmony_ci /* Locate the driver that corresponds to the message */ 858c2ecf20Sopenharmony_ci err = -ESRCH; 868c2ecf20Sopenharmony_ci break; 878c2ecf20Sopenharmony_ci default: 888c2ecf20Sopenharmony_ci err = -EBADR; 898c2ecf20Sopenharmony_ci break; 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci if (err) 928c2ecf20Sopenharmony_ci printk(KERN_WARNING "%s: Msgtype %d failed - err %d\n", 938c2ecf20Sopenharmony_ci __func__, hdr->msgtype, err); 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci else 968c2ecf20Sopenharmony_ci err = -ENOENT; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cinext_msg: 998c2ecf20Sopenharmony_ci if ((err) || (nlh->nlmsg_flags & NLM_F_ACK)) 1008c2ecf20Sopenharmony_ci netlink_ack(skb, nlh, err, NULL); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci skb_pull(skb, rlen); 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/** 1078c2ecf20Sopenharmony_ci * scsi_netlink_init - Called by SCSI subsystem to initialize 1088c2ecf20Sopenharmony_ci * the SCSI transport netlink interface 1098c2ecf20Sopenharmony_ci * 1108c2ecf20Sopenharmony_ci **/ 1118c2ecf20Sopenharmony_civoid 1128c2ecf20Sopenharmony_ciscsi_netlink_init(void) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci struct netlink_kernel_cfg cfg = { 1158c2ecf20Sopenharmony_ci .input = scsi_nl_rcv_msg, 1168c2ecf20Sopenharmony_ci .groups = SCSI_NL_GRP_CNT, 1178c2ecf20Sopenharmony_ci }; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci scsi_nl_sock = netlink_kernel_create(&init_net, NETLINK_SCSITRANSPORT, 1208c2ecf20Sopenharmony_ci &cfg); 1218c2ecf20Sopenharmony_ci if (!scsi_nl_sock) { 1228c2ecf20Sopenharmony_ci printk(KERN_ERR "%s: register of receive handler failed\n", 1238c2ecf20Sopenharmony_ci __func__); 1248c2ecf20Sopenharmony_ci return; 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci return; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci/** 1328c2ecf20Sopenharmony_ci * scsi_netlink_exit - Called by SCSI subsystem to disable the SCSI transport netlink interface 1338c2ecf20Sopenharmony_ci * 1348c2ecf20Sopenharmony_ci **/ 1358c2ecf20Sopenharmony_civoid 1368c2ecf20Sopenharmony_ciscsi_netlink_exit(void) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci if (scsi_nl_sock) { 1398c2ecf20Sopenharmony_ci netlink_kernel_release(scsi_nl_sock); 1408c2ecf20Sopenharmony_ci } 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci return; 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 145