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>
24 #include <linux/delay.h>
26 #include <net/netlink.h>
28 #include <scsi/scsi_netlink.h>
29 #include "scsi_priv.h"
31 struct sock
*scsi_nl_sock
= NULL
;
32 EXPORT_SYMBOL_GPL(scsi_nl_sock
);
34 static DEFINE_SPINLOCK(scsi_nl_lock
);
35 static struct list_head scsi_nl_drivers
;
37 static u32 scsi_nl_state
;
38 #define STATE_EHANDLER_BSY 0x00000001
40 struct scsi_nl_transport
{
41 int (*msg_handler
)(struct sk_buff
*);
42 void (*event_handler
)(struct notifier_block
*, unsigned long, void *);
47 /* flags values (bit flags) */
48 #define HANDLER_DELETING 0x1
50 static struct scsi_nl_transport transports
[SCSI_NL_MAX_TRANSPORTS
] =
55 struct list_head next
;
56 int (*dmsg_handler
)(struct Scsi_Host
*shost
, void *payload
,
58 void (*devt_handler
)(struct notifier_block
*nb
,
59 unsigned long event
, void *notify_ptr
);
60 struct scsi_host_template
*hostt
;
69 * scsi_nl_rcv_msg - Receive message handler.
70 * @skb: socket receive buffer
72 * Description: Extracts message from a receive buffer.
73 * Validates message header and calls appropriate transport message handler
78 scsi_nl_rcv_msg(struct sk_buff
*skb
)
81 struct scsi_nl_hdr
*hdr
;
86 while (skb
->len
>= NLMSG_SPACE(0)) {
90 if ((nlh
->nlmsg_len
< (sizeof(*nlh
) + sizeof(*hdr
))) ||
91 (skb
->len
< nlh
->nlmsg_len
)) {
92 printk(KERN_WARNING
"%s: discarding partial skb\n",
97 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
101 if (nlh
->nlmsg_type
!= SCSI_TRANSPORT_MSG
) {
106 hdr
= NLMSG_DATA(nlh
);
107 if ((hdr
->version
!= SCSI_NL_VERSION
) ||
108 (hdr
->magic
!= SCSI_NL_MAGIC
)) {
113 if (security_netlink_recv(skb
, CAP_SYS_ADMIN
)) {
118 if (nlh
->nlmsg_len
< (sizeof(*nlh
) + hdr
->msglen
)) {
119 printk(KERN_WARNING
"%s: discarding partial message\n",
125 * Deliver message to the appropriate transport
127 spin_lock_irqsave(&scsi_nl_lock
, flags
);
129 tport
= hdr
->transport
;
130 if ((tport
< SCSI_NL_MAX_TRANSPORTS
) &&
131 !(transports
[tport
].flags
& HANDLER_DELETING
) &&
132 (transports
[tport
].msg_handler
)) {
133 transports
[tport
].refcnt
++;
134 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
135 err
= transports
[tport
].msg_handler(skb
);
136 spin_lock_irqsave(&scsi_nl_lock
, flags
);
137 transports
[tport
].refcnt
--;
141 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
144 if ((err
) || (nlh
->nlmsg_flags
& NLM_F_ACK
))
145 netlink_ack(skb
, nlh
, err
);
153 * scsi_nl_rcv_event - Event handler for a netlink socket.
154 * @this: event notifier block
156 * @ptr: event payload
160 scsi_nl_rcv_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
162 struct netlink_notify
*n
= ptr
;
163 struct scsi_nl_drvr
*driver
;
167 if (n
->protocol
!= NETLINK_SCSITRANSPORT
)
170 spin_lock_irqsave(&scsi_nl_lock
, flags
);
171 scsi_nl_state
|= STATE_EHANDLER_BSY
;
174 * Pass event on to any transports that may be listening
176 for (tport
= 0; tport
< SCSI_NL_MAX_TRANSPORTS
; tport
++) {
177 if (!(transports
[tport
].flags
& HANDLER_DELETING
) &&
178 (transports
[tport
].event_handler
)) {
179 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
180 transports
[tport
].event_handler(this, event
, ptr
);
181 spin_lock_irqsave(&scsi_nl_lock
, flags
);
186 * Pass event on to any drivers that may be listening
188 list_for_each_entry(driver
, &scsi_nl_drivers
, next
) {
189 if (!(driver
->flags
& HANDLER_DELETING
) &&
190 (driver
->devt_handler
)) {
191 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
192 driver
->devt_handler(this, event
, ptr
);
193 spin_lock_irqsave(&scsi_nl_lock
, flags
);
197 scsi_nl_state
&= ~STATE_EHANDLER_BSY
;
198 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
203 static struct notifier_block scsi_netlink_notifier
= {
204 .notifier_call
= scsi_nl_rcv_event
,
209 * GENERIC SCSI transport receive and event handlers
213 * scsi_generic_msg_handler - receive message handler for GENERIC transport
216 * @skb: socket receive buffer
220 scsi_generic_msg_handler(struct sk_buff
*skb
)
222 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
223 struct scsi_nl_hdr
*snlh
= NLMSG_DATA(nlh
);
224 struct scsi_nl_drvr
*driver
;
225 struct Scsi_Host
*shost
;
227 int err
= 0, match
, pid
;
229 pid
= NETLINK_CREDS(skb
)->pid
;
231 switch (snlh
->msgtype
) {
232 case SCSI_NL_SHOST_VENDOR
:
234 struct scsi_nl_host_vendor_msg
*msg
= NLMSG_DATA(nlh
);
236 /* Locate the driver that corresponds to the message */
237 spin_lock_irqsave(&scsi_nl_lock
, flags
);
239 list_for_each_entry(driver
, &scsi_nl_drivers
, next
) {
240 if (driver
->vendor_id
== msg
->vendor_id
) {
246 if ((!match
) || (!driver
->dmsg_handler
)) {
247 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
252 if (driver
->flags
& HANDLER_DELETING
) {
253 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
259 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
262 /* if successful, scsi_host_lookup takes a shost reference */
263 shost
= scsi_host_lookup(msg
->host_no
);
269 /* is this host owned by the vendor ? */
270 if (shost
->hostt
!= driver
->hostt
) {
275 /* pass message on to the driver */
276 err
= driver
->dmsg_handler(shost
, (void *)&msg
[1],
277 msg
->vmsg_datalen
, pid
);
280 /* release reference by scsi_host_lookup */
281 scsi_host_put(shost
);
284 /* release our own reference on the registration object */
285 spin_lock_irqsave(&scsi_nl_lock
, flags
);
287 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
298 printk(KERN_WARNING
"%s: Msgtype %d failed - err %d\n",
299 __func__
, snlh
->msgtype
, err
);
305 * scsi_nl_add_transport -
306 * Registers message and event handlers for a transport. Enables
307 * receipt of netlink messages and events to a transport.
309 * @tport: transport registering handlers
310 * @msg_handler: receive message handler callback
311 * @event_handler: receive event handler callback
314 scsi_nl_add_transport(u8 tport
,
315 int (*msg_handler
)(struct sk_buff
*),
316 void (*event_handler
)(struct notifier_block
*, unsigned long, void *))
321 if (tport
>= SCSI_NL_MAX_TRANSPORTS
)
324 spin_lock_irqsave(&scsi_nl_lock
, flags
);
326 if (scsi_nl_state
& STATE_EHANDLER_BSY
) {
327 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
329 spin_lock_irqsave(&scsi_nl_lock
, flags
);
332 if (transports
[tport
].msg_handler
|| transports
[tport
].event_handler
) {
337 transports
[tport
].msg_handler
= msg_handler
;
338 transports
[tport
].event_handler
= event_handler
;
339 transports
[tport
].flags
= 0;
340 transports
[tport
].refcnt
= 0;
343 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
347 EXPORT_SYMBOL_GPL(scsi_nl_add_transport
);
351 * scsi_nl_remove_transport -
352 * Disable transport receiption of messages and events
354 * @tport: transport deregistering handlers
358 scsi_nl_remove_transport(u8 tport
)
362 spin_lock_irqsave(&scsi_nl_lock
, flags
);
363 if (scsi_nl_state
& STATE_EHANDLER_BSY
) {
364 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
366 spin_lock_irqsave(&scsi_nl_lock
, flags
);
369 if (tport
< SCSI_NL_MAX_TRANSPORTS
) {
370 transports
[tport
].flags
|= HANDLER_DELETING
;
372 while (transports
[tport
].refcnt
!= 0) {
373 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
374 schedule_timeout_uninterruptible(HZ
/4);
375 spin_lock_irqsave(&scsi_nl_lock
, flags
);
377 transports
[tport
].msg_handler
= NULL
;
378 transports
[tport
].event_handler
= NULL
;
379 transports
[tport
].flags
= 0;
382 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
386 EXPORT_SYMBOL_GPL(scsi_nl_remove_transport
);
390 * scsi_nl_add_driver -
391 * A driver is registering its interfaces for SCSI netlink messages
393 * @vendor_id: A unique identification value for the driver.
394 * @hostt: address of the driver's host template. Used
395 * to verify an shost is bound to the driver
396 * @nlmsg_handler: receive message handler callback
397 * @nlevt_handler: receive event handler callback
401 * error result otherwise
404 scsi_nl_add_driver(u64 vendor_id
, struct scsi_host_template
*hostt
,
405 int (*nlmsg_handler
)(struct Scsi_Host
*shost
, void *payload
,
407 void (*nlevt_handler
)(struct notifier_block
*nb
,
408 unsigned long event
, void *notify_ptr
))
410 struct scsi_nl_drvr
*driver
;
413 driver
= kzalloc(sizeof(*driver
), GFP_KERNEL
);
414 if (unlikely(!driver
)) {
415 printk(KERN_ERR
"%s: allocation failure\n", __func__
);
419 driver
->dmsg_handler
= nlmsg_handler
;
420 driver
->devt_handler
= nlevt_handler
;
421 driver
->hostt
= hostt
;
422 driver
->vendor_id
= vendor_id
;
424 spin_lock_irqsave(&scsi_nl_lock
, flags
);
425 if (scsi_nl_state
& STATE_EHANDLER_BSY
) {
426 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
428 spin_lock_irqsave(&scsi_nl_lock
, flags
);
430 list_add_tail(&driver
->next
, &scsi_nl_drivers
);
431 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
435 EXPORT_SYMBOL_GPL(scsi_nl_add_driver
);
439 * scsi_nl_remove_driver -
440 * An driver is unregistering with the SCSI netlink messages
442 * @vendor_id: The unique identification value for the driver.
445 scsi_nl_remove_driver(u64 vendor_id
)
447 struct scsi_nl_drvr
*driver
;
450 spin_lock_irqsave(&scsi_nl_lock
, flags
);
451 if (scsi_nl_state
& STATE_EHANDLER_BSY
) {
452 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
454 spin_lock_irqsave(&scsi_nl_lock
, flags
);
457 list_for_each_entry(driver
, &scsi_nl_drivers
, next
) {
458 if (driver
->vendor_id
== vendor_id
) {
459 driver
->flags
|= HANDLER_DELETING
;
460 while (driver
->refcnt
!= 0) {
461 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
462 schedule_timeout_uninterruptible(HZ
/4);
463 spin_lock_irqsave(&scsi_nl_lock
, flags
);
465 list_del(&driver
->next
);
467 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
472 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
474 printk(KERN_ERR
"%s: removal of driver failed - vendor_id 0x%llx\n",
475 __func__
, (unsigned long long)vendor_id
);
478 EXPORT_SYMBOL_GPL(scsi_nl_remove_driver
);
482 * scsi_netlink_init - Called by SCSI subsystem to intialize
483 * the SCSI transport netlink interface
487 scsi_netlink_init(void)
491 INIT_LIST_HEAD(&scsi_nl_drivers
);
493 error
= netlink_register_notifier(&scsi_netlink_notifier
);
495 printk(KERN_ERR
"%s: register of event handler failed - %d\n",
500 scsi_nl_sock
= netlink_kernel_create(&init_net
, NETLINK_SCSITRANSPORT
,
501 SCSI_NL_GRP_CNT
, scsi_nl_rcv_msg
, NULL
,
504 printk(KERN_ERR
"%s: register of recieve handler failed\n",
506 netlink_unregister_notifier(&scsi_netlink_notifier
);
510 /* Register the entry points for the generic SCSI transport */
511 error
= scsi_nl_add_transport(SCSI_NL_TRANSPORT
,
512 scsi_generic_msg_handler
, NULL
);
514 printk(KERN_ERR
"%s: register of GENERIC transport handler"
515 " failed - %d\n", __func__
, error
);
521 * scsi_netlink_exit - Called by SCSI subsystem to disable the SCSI transport netlink interface
525 scsi_netlink_exit(void)
527 scsi_nl_remove_transport(SCSI_NL_TRANSPORT
);
530 netlink_kernel_release(scsi_nl_sock
);
531 netlink_unregister_notifier(&scsi_netlink_notifier
);
539 * Exported Interfaces
543 * scsi_nl_send_transport_msg -
544 * Generic function to send a single message from a SCSI transport to
547 * @pid: receiving pid
548 * @hdr: message payload
552 scsi_nl_send_transport_msg(u32 pid
, struct scsi_nl_hdr
*hdr
)
555 struct nlmsghdr
*nlh
;
563 fn
= "netlink socket";
567 len
= NLMSG_SPACE(hdr
->msglen
);
568 skblen
= NLMSG_SPACE(len
);
570 skb
= alloc_skb(skblen
, GFP_KERNEL
);
577 nlh
= nlmsg_put(skb
, pid
, 0, SCSI_TRANSPORT_MSG
, len
- sizeof(*nlh
), 0);
583 datab
= NLMSG_DATA(nlh
);
584 memcpy(datab
, hdr
, hdr
->msglen
);
586 err
= nlmsg_unicast(scsi_nl_sock
, skb
, pid
);
588 fn
= "nlmsg_unicast";
589 /* nlmsg_unicast already kfree_skb'd */
599 "%s: Dropped Message : pid %d Transport %d, msgtype x%x, "
600 "msglen %d: %s : err %d\n",
601 __func__
, pid
, hdr
->transport
, hdr
->msgtype
, hdr
->msglen
,
605 EXPORT_SYMBOL_GPL(scsi_nl_send_transport_msg
);
609 * scsi_nl_send_vendor_msg - called to send a shost vendor unique message
610 * to a specific process id.
612 * @pid: process id of the receiver
613 * @host_no: host # sending the message
614 * @vendor_id: unique identifier for the driver's vendor
615 * @data_len: amount, in bytes, of vendor unique payload data
616 * @data_buf: pointer to vendor unique data buffer
619 * 0 on succesful return
620 * otherwise, failing error code
623 * This routine assumes no locks are held on entry.
626 scsi_nl_send_vendor_msg(u32 pid
, unsigned short host_no
, u64 vendor_id
,
627 char *data_buf
, u32 data_len
)
630 struct nlmsghdr
*nlh
;
631 struct scsi_nl_host_vendor_msg
*msg
;
637 goto send_vendor_fail
;
640 len
= SCSI_NL_MSGALIGN(sizeof(*msg
) + data_len
);
641 skblen
= NLMSG_SPACE(len
);
643 skb
= alloc_skb(skblen
, GFP_KERNEL
);
646 goto send_vendor_fail
;
649 nlh
= nlmsg_put(skb
, 0, 0, SCSI_TRANSPORT_MSG
,
650 skblen
- sizeof(*nlh
), 0);
653 goto send_vendor_fail_skb
;
655 msg
= NLMSG_DATA(nlh
);
657 INIT_SCSI_NL_HDR(&msg
->snlh
, SCSI_NL_TRANSPORT
,
658 SCSI_NL_SHOST_VENDOR
, len
);
659 msg
->vendor_id
= vendor_id
;
660 msg
->host_no
= host_no
;
661 msg
->vmsg_datalen
= data_len
; /* bytes */
662 memcpy(&msg
[1], data_buf
, data_len
);
664 err
= nlmsg_unicast(scsi_nl_sock
, skb
, pid
);
666 /* nlmsg_multicast already kfree_skb'd */
667 goto send_vendor_fail
;
671 send_vendor_fail_skb
:
675 "%s: Dropped SCSI Msg : host %d vendor_unique - err %d\n",
676 __func__
, host_no
, err
);
679 EXPORT_SYMBOL(scsi_nl_send_vendor_msg
);