2 * scsi_netlink.c - SCSI Transport Netlink Interface
4 * Copyright (C) 2006 James Smart, Emulex Corporation
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.
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.
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
21 #include <linux/time.h>
22 #include <linux/jiffies.h>
23 #include <linux/security.h>
25 #include <net/netlink.h>
27 #include <scsi/scsi_netlink.h>
28 #include "scsi_priv.h"
30 struct sock *scsi_nl_sock = NULL;
31 EXPORT_SYMBOL_GPL(scsi_nl_sock);
36 * Receive message handler. Extracts message from a receive buffer.
37 * Validates message header and calls appropriate transport message handler
39 * @skb: socket receive buffer
43 scsi_nl_rcv_msg(struct sk_buff *skb)
46 struct scsi_nl_hdr *hdr;
50 while (skb->len >= NLMSG_SPACE(0)) {
53 nlh = (struct nlmsghdr *) skb->data;
54 if ((nlh->nlmsg_len < (sizeof(*nlh) + sizeof(*hdr))) ||
55 (skb->len < nlh->nlmsg_len)) {
56 printk(KERN_WARNING "%s: discarding partial skb\n",
61 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
65 if (nlh->nlmsg_type != SCSI_TRANSPORT_MSG) {
70 hdr = NLMSG_DATA(nlh);
71 if ((hdr->version != SCSI_NL_VERSION) ||
72 (hdr->magic != SCSI_NL_MAGIC)) {
77 if (security_netlink_recv(skb, CAP_SYS_ADMIN)) {
82 if (nlh->nlmsg_len < (sizeof(*nlh) + hdr->msglen)) {
83 printk(KERN_WARNING "%s: discarding partial message\n",
89 * We currently don't support anyone sending us a message
93 if ((err) || (nlh->nlmsg_flags & NLM_F_ACK))
94 netlink_ack(skb, nlh, err);
103 * Receive handler for a socket. Extracts a received message buffer from
104 * the socket, and starts message processing.
111 scsi_nl_rcv(struct sock *sk, int len)
115 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
116 scsi_nl_rcv_msg(skb);
123 * scsi_nl_rcv_event -
124 * Event handler for a netlink socket.
126 * @this: event notifier block
128 * @ptr: event payload
132 scsi_nl_rcv_event(struct notifier_block *this, unsigned long event, void *ptr)
134 struct netlink_notify *n = ptr;
136 if (n->protocol != NETLINK_SCSITRANSPORT)
140 * Currently, we are not tracking PID's, etc. There is nothing
147 static struct notifier_block scsi_netlink_notifier = {
148 .notifier_call = scsi_nl_rcv_event,
153 * scsi_netlink_init -
154 * Called by SCSI subsystem to intialize the SCSI transport netlink
159 scsi_netlink_init(void)
163 error = netlink_register_notifier(&scsi_netlink_notifier);
165 printk(KERN_ERR "%s: register of event handler failed - %d\n",
166 __FUNCTION__, error);
170 scsi_nl_sock = netlink_kernel_create(NETLINK_SCSITRANSPORT,
171 SCSI_NL_GRP_CNT, scsi_nl_rcv, THIS_MODULE);
173 printk(KERN_ERR "%s: register of recieve handler failed\n",
175 netlink_unregister_notifier(&scsi_netlink_notifier);
183 * scsi_netlink_exit -
184 * Called by SCSI subsystem to disable the SCSI transport netlink
189 scsi_netlink_exit(void)
192 sock_release(scsi_nl_sock->sk_socket);
193 netlink_unregister_notifier(&scsi_netlink_notifier);