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>
25 #include <linux/slab.h>
27 #include <net/netlink.h>
29 #include <scsi/scsi_netlink.h>
30 #include "scsi_priv.h"
32 struct sock
*scsi_nl_sock
= NULL
;
33 EXPORT_SYMBOL_GPL(scsi_nl_sock
);
35 static DEFINE_SPINLOCK(scsi_nl_lock
);
36 static struct list_head scsi_nl_drivers
;
38 static u32 scsi_nl_state
;
39 #define STATE_EHANDLER_BSY 0x00000001
41 struct scsi_nl_transport
{
42 int (*msg_handler
)(struct sk_buff
*);
43 void (*event_handler
)(struct notifier_block
*, unsigned long, void *);
48 /* flags values (bit flags) */
49 #define HANDLER_DELETING 0x1
51 static struct scsi_nl_transport transports
[SCSI_NL_MAX_TRANSPORTS
] =
56 struct list_head next
;
57 int (*dmsg_handler
)(struct Scsi_Host
*shost
, void *payload
,
59 void (*devt_handler
)(struct notifier_block
*nb
,
60 unsigned long event
, void *notify_ptr
);
61 struct scsi_host_template
*hostt
;
70 * scsi_nl_rcv_msg - Receive message handler.
71 * @skb: socket receive buffer
73 * Description: Extracts message from a receive buffer.
74 * Validates message header and calls appropriate transport message handler
79 scsi_nl_rcv_msg(struct sk_buff
*skb
)
82 struct scsi_nl_hdr
*hdr
;
87 while (skb
->len
>= NLMSG_SPACE(0)) {
91 if ((nlh
->nlmsg_len
< (sizeof(*nlh
) + sizeof(*hdr
))) ||
92 (skb
->len
< nlh
->nlmsg_len
)) {
93 printk(KERN_WARNING
"%s: discarding partial skb\n",
98 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
102 if (nlh
->nlmsg_type
!= SCSI_TRANSPORT_MSG
) {
107 hdr
= NLMSG_DATA(nlh
);
108 if ((hdr
->version
!= SCSI_NL_VERSION
) ||
109 (hdr
->magic
!= SCSI_NL_MAGIC
)) {
114 if (security_netlink_recv(skb
, CAP_SYS_ADMIN
)) {
119 if (nlh
->nlmsg_len
< (sizeof(*nlh
) + hdr
->msglen
)) {
120 printk(KERN_WARNING
"%s: discarding partial message\n",
126 * Deliver message to the appropriate transport
128 spin_lock_irqsave(&scsi_nl_lock
, flags
);
130 tport
= hdr
->transport
;
131 if ((tport
< SCSI_NL_MAX_TRANSPORTS
) &&
132 !(transports
[tport
].flags
& HANDLER_DELETING
) &&
133 (transports
[tport
].msg_handler
)) {
134 transports
[tport
].refcnt
++;
135 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
136 err
= transports
[tport
].msg_handler(skb
);
137 spin_lock_irqsave(&scsi_nl_lock
, flags
);
138 transports
[tport
].refcnt
--;
142 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
145 if ((err
) || (nlh
->nlmsg_flags
& NLM_F_ACK
))
146 netlink_ack(skb
, nlh
, err
);
154 * scsi_nl_rcv_event - Event handler for a netlink socket.
155 * @this: event notifier block
157 * @ptr: event payload
161 scsi_nl_rcv_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
163 struct netlink_notify
*n
= ptr
;
164 struct scsi_nl_drvr
*driver
;
168 if (n
->protocol
!= NETLINK_SCSITRANSPORT
)
171 spin_lock_irqsave(&scsi_nl_lock
, flags
);
172 scsi_nl_state
|= STATE_EHANDLER_BSY
;
175 * Pass event on to any transports that may be listening
177 for (tport
= 0; tport
< SCSI_NL_MAX_TRANSPORTS
; tport
++) {
178 if (!(transports
[tport
].flags
& HANDLER_DELETING
) &&
179 (transports
[tport
].event_handler
)) {
180 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
181 transports
[tport
].event_handler(this, event
, ptr
);
182 spin_lock_irqsave(&scsi_nl_lock
, flags
);
187 * Pass event on to any drivers that may be listening
189 list_for_each_entry(driver
, &scsi_nl_drivers
, next
) {
190 if (!(driver
->flags
& HANDLER_DELETING
) &&
191 (driver
->devt_handler
)) {
192 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
193 driver
->devt_handler(this, event
, ptr
);
194 spin_lock_irqsave(&scsi_nl_lock
, flags
);
198 scsi_nl_state
&= ~STATE_EHANDLER_BSY
;
199 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
204 static struct notifier_block scsi_netlink_notifier
= {
205 .notifier_call
= scsi_nl_rcv_event
,
210 * GENERIC SCSI transport receive and event handlers
214 * scsi_generic_msg_handler - receive message handler for GENERIC transport messages
215 * @skb: socket receive buffer
218 scsi_generic_msg_handler(struct sk_buff
*skb
)
220 struct nlmsghdr
*nlh
= nlmsg_hdr(skb
);
221 struct scsi_nl_hdr
*snlh
= NLMSG_DATA(nlh
);
222 struct scsi_nl_drvr
*driver
;
223 struct Scsi_Host
*shost
;
225 int err
= 0, match
, pid
;
227 pid
= NETLINK_CREDS(skb
)->pid
;
229 switch (snlh
->msgtype
) {
230 case SCSI_NL_SHOST_VENDOR
:
232 struct scsi_nl_host_vendor_msg
*msg
= NLMSG_DATA(nlh
);
234 /* Locate the driver that corresponds to the message */
235 spin_lock_irqsave(&scsi_nl_lock
, flags
);
237 list_for_each_entry(driver
, &scsi_nl_drivers
, next
) {
238 if (driver
->vendor_id
== msg
->vendor_id
) {
244 if ((!match
) || (!driver
->dmsg_handler
)) {
245 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
250 if (driver
->flags
& HANDLER_DELETING
) {
251 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
257 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
260 /* if successful, scsi_host_lookup takes a shost reference */
261 shost
= scsi_host_lookup(msg
->host_no
);
267 /* is this host owned by the vendor ? */
268 if (shost
->hostt
!= driver
->hostt
) {
273 /* pass message on to the driver */
274 err
= driver
->dmsg_handler(shost
, (void *)&msg
[1],
275 msg
->vmsg_datalen
, pid
);
278 /* release reference by scsi_host_lookup */
279 scsi_host_put(shost
);
282 /* release our own reference on the registration object */
283 spin_lock_irqsave(&scsi_nl_lock
, flags
);
285 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
296 printk(KERN_WARNING
"%s: Msgtype %d failed - err %d\n",
297 __func__
, snlh
->msgtype
, err
);
303 * scsi_nl_add_transport -
304 * Registers message and event handlers for a transport. Enables
305 * receipt of netlink messages and events to a transport.
307 * @tport: transport registering handlers
308 * @msg_handler: receive message handler callback
309 * @event_handler: receive event handler callback
312 scsi_nl_add_transport(u8 tport
,
313 int (*msg_handler
)(struct sk_buff
*),
314 void (*event_handler
)(struct notifier_block
*, unsigned long, void *))
319 if (tport
>= SCSI_NL_MAX_TRANSPORTS
)
322 spin_lock_irqsave(&scsi_nl_lock
, flags
);
324 if (scsi_nl_state
& STATE_EHANDLER_BSY
) {
325 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
327 spin_lock_irqsave(&scsi_nl_lock
, flags
);
330 if (transports
[tport
].msg_handler
|| transports
[tport
].event_handler
) {
335 transports
[tport
].msg_handler
= msg_handler
;
336 transports
[tport
].event_handler
= event_handler
;
337 transports
[tport
].flags
= 0;
338 transports
[tport
].refcnt
= 0;
341 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
345 EXPORT_SYMBOL_GPL(scsi_nl_add_transport
);
349 * scsi_nl_remove_transport -
350 * Disable transport receiption of messages and events
352 * @tport: transport deregistering handlers
356 scsi_nl_remove_transport(u8 tport
)
360 spin_lock_irqsave(&scsi_nl_lock
, flags
);
361 if (scsi_nl_state
& STATE_EHANDLER_BSY
) {
362 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
364 spin_lock_irqsave(&scsi_nl_lock
, flags
);
367 if (tport
< SCSI_NL_MAX_TRANSPORTS
) {
368 transports
[tport
].flags
|= HANDLER_DELETING
;
370 while (transports
[tport
].refcnt
!= 0) {
371 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
372 schedule_timeout_uninterruptible(HZ
/4);
373 spin_lock_irqsave(&scsi_nl_lock
, flags
);
375 transports
[tport
].msg_handler
= NULL
;
376 transports
[tport
].event_handler
= NULL
;
377 transports
[tport
].flags
= 0;
380 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
384 EXPORT_SYMBOL_GPL(scsi_nl_remove_transport
);
388 * scsi_nl_add_driver -
389 * A driver is registering its interfaces for SCSI netlink messages
391 * @vendor_id: A unique identification value for the driver.
392 * @hostt: address of the driver's host template. Used
393 * to verify an shost is bound to the driver
394 * @nlmsg_handler: receive message handler callback
395 * @nlevt_handler: receive event handler callback
399 * error result otherwise
402 scsi_nl_add_driver(u64 vendor_id
, struct scsi_host_template
*hostt
,
403 int (*nlmsg_handler
)(struct Scsi_Host
*shost
, void *payload
,
405 void (*nlevt_handler
)(struct notifier_block
*nb
,
406 unsigned long event
, void *notify_ptr
))
408 struct scsi_nl_drvr
*driver
;
411 driver
= kzalloc(sizeof(*driver
), GFP_KERNEL
);
412 if (unlikely(!driver
)) {
413 printk(KERN_ERR
"%s: allocation failure\n", __func__
);
417 driver
->dmsg_handler
= nlmsg_handler
;
418 driver
->devt_handler
= nlevt_handler
;
419 driver
->hostt
= hostt
;
420 driver
->vendor_id
= vendor_id
;
422 spin_lock_irqsave(&scsi_nl_lock
, flags
);
423 if (scsi_nl_state
& STATE_EHANDLER_BSY
) {
424 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
426 spin_lock_irqsave(&scsi_nl_lock
, flags
);
428 list_add_tail(&driver
->next
, &scsi_nl_drivers
);
429 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
433 EXPORT_SYMBOL_GPL(scsi_nl_add_driver
);
437 * scsi_nl_remove_driver -
438 * An driver is unregistering with the SCSI netlink messages
440 * @vendor_id: The unique identification value for the driver.
443 scsi_nl_remove_driver(u64 vendor_id
)
445 struct scsi_nl_drvr
*driver
;
448 spin_lock_irqsave(&scsi_nl_lock
, flags
);
449 if (scsi_nl_state
& STATE_EHANDLER_BSY
) {
450 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
452 spin_lock_irqsave(&scsi_nl_lock
, flags
);
455 list_for_each_entry(driver
, &scsi_nl_drivers
, next
) {
456 if (driver
->vendor_id
== vendor_id
) {
457 driver
->flags
|= HANDLER_DELETING
;
458 while (driver
->refcnt
!= 0) {
459 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
460 schedule_timeout_uninterruptible(HZ
/4);
461 spin_lock_irqsave(&scsi_nl_lock
, flags
);
463 list_del(&driver
->next
);
465 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
470 spin_unlock_irqrestore(&scsi_nl_lock
, flags
);
472 printk(KERN_ERR
"%s: removal of driver failed - vendor_id 0x%llx\n",
473 __func__
, (unsigned long long)vendor_id
);
476 EXPORT_SYMBOL_GPL(scsi_nl_remove_driver
);
480 * scsi_netlink_init - Called by SCSI subsystem to intialize
481 * the SCSI transport netlink interface
485 scsi_netlink_init(void)
489 INIT_LIST_HEAD(&scsi_nl_drivers
);
491 error
= netlink_register_notifier(&scsi_netlink_notifier
);
493 printk(KERN_ERR
"%s: register of event handler failed - %d\n",
498 scsi_nl_sock
= netlink_kernel_create(&init_net
, NETLINK_SCSITRANSPORT
,
499 SCSI_NL_GRP_CNT
, scsi_nl_rcv_msg
, NULL
,
502 printk(KERN_ERR
"%s: register of recieve handler failed\n",
504 netlink_unregister_notifier(&scsi_netlink_notifier
);
508 /* Register the entry points for the generic SCSI transport */
509 error
= scsi_nl_add_transport(SCSI_NL_TRANSPORT
,
510 scsi_generic_msg_handler
, NULL
);
512 printk(KERN_ERR
"%s: register of GENERIC transport handler"
513 " failed - %d\n", __func__
, error
);
519 * scsi_netlink_exit - Called by SCSI subsystem to disable the SCSI transport netlink interface
523 scsi_netlink_exit(void)
525 scsi_nl_remove_transport(SCSI_NL_TRANSPORT
);
528 netlink_kernel_release(scsi_nl_sock
);
529 netlink_unregister_notifier(&scsi_netlink_notifier
);
537 * Exported Interfaces
541 * scsi_nl_send_transport_msg -
542 * Generic function to send a single message from a SCSI transport to
545 * @pid: receiving pid
546 * @hdr: message payload
550 scsi_nl_send_transport_msg(u32 pid
, struct scsi_nl_hdr
*hdr
)
553 struct nlmsghdr
*nlh
;
561 fn
= "netlink socket";
565 len
= NLMSG_SPACE(hdr
->msglen
);
566 skblen
= NLMSG_SPACE(len
);
568 skb
= alloc_skb(skblen
, GFP_KERNEL
);
575 nlh
= nlmsg_put(skb
, pid
, 0, SCSI_TRANSPORT_MSG
, len
- sizeof(*nlh
), 0);
581 datab
= NLMSG_DATA(nlh
);
582 memcpy(datab
, hdr
, hdr
->msglen
);
584 err
= nlmsg_unicast(scsi_nl_sock
, skb
, pid
);
586 fn
= "nlmsg_unicast";
587 /* nlmsg_unicast already kfree_skb'd */
597 "%s: Dropped Message : pid %d Transport %d, msgtype x%x, "
598 "msglen %d: %s : err %d\n",
599 __func__
, pid
, hdr
->transport
, hdr
->msgtype
, hdr
->msglen
,
603 EXPORT_SYMBOL_GPL(scsi_nl_send_transport_msg
);
607 * scsi_nl_send_vendor_msg - called to send a shost vendor unique message
608 * to a specific process id.
610 * @pid: process id of the receiver
611 * @host_no: host # sending the message
612 * @vendor_id: unique identifier for the driver's vendor
613 * @data_len: amount, in bytes, of vendor unique payload data
614 * @data_buf: pointer to vendor unique data buffer
617 * 0 on successful return
618 * otherwise, failing error code
621 * This routine assumes no locks are held on entry.
624 scsi_nl_send_vendor_msg(u32 pid
, unsigned short host_no
, u64 vendor_id
,
625 char *data_buf
, u32 data_len
)
628 struct nlmsghdr
*nlh
;
629 struct scsi_nl_host_vendor_msg
*msg
;
635 goto send_vendor_fail
;
638 len
= SCSI_NL_MSGALIGN(sizeof(*msg
) + data_len
);
639 skblen
= NLMSG_SPACE(len
);
641 skb
= alloc_skb(skblen
, GFP_KERNEL
);
644 goto send_vendor_fail
;
647 nlh
= nlmsg_put(skb
, 0, 0, SCSI_TRANSPORT_MSG
,
648 skblen
- sizeof(*nlh
), 0);
651 goto send_vendor_fail_skb
;
653 msg
= NLMSG_DATA(nlh
);
655 INIT_SCSI_NL_HDR(&msg
->snlh
, SCSI_NL_TRANSPORT
,
656 SCSI_NL_SHOST_VENDOR
, len
);
657 msg
->vendor_id
= vendor_id
;
658 msg
->host_no
= host_no
;
659 msg
->vmsg_datalen
= data_len
; /* bytes */
660 memcpy(&msg
[1], data_buf
, data_len
);
662 err
= nlmsg_unicast(scsi_nl_sock
, skb
, pid
);
664 /* nlmsg_multicast already kfree_skb'd */
665 goto send_vendor_fail
;
669 send_vendor_fail_skb
:
673 "%s: Dropped SCSI Msg : host %d vendor_unique - err %d\n",
674 __func__
, host_no
, err
);
677 EXPORT_SYMBOL(scsi_nl_send_vendor_msg
);