25cf1a301a396c38e8adf52c15f537b80d2483f7jl * CDDL HEADER START
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * The contents of this file are subject to the terms of the
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Common Development and Distribution License (the "License").
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * You may not use this file except in compliance with the License.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * See the License for the specific language governing permissions
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * and limitations under the License.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * When distributing Covered Code, include this CDDL HEADER in each
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * If applicable, add the following below this CDDL HEADER, with the
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * fields enclosed by brackets "[]" replaced with your own identifying
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * information: Portions Copyright [yyyy] [name of copyright owner]
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * CDDL HEADER END
6074f19f4f7fc46d66216416827712a7511abffbZach Kissel * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Use is subject to license terms.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * OPL IPSec Key Management Driver.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * This driver runs on a OPL Domain. It processes requests received
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * from the OPL Service Processor (SP) via mailbox message. It passes
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * these requests to the sckmd daemon by means of an /ioctl interface.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Requests received from the SP consist of IPsec security associations
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * (SAs) needed to secure the communication between SC and Domain daemons
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * communicating using DSCP.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl#define OKM_MB_TOUT 10000000 /* Mailbox timeout (10sec) */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Prototypes for the module related functions.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlint okm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result);
25cf1a301a396c38e8adf52c15f537b80d2483f7jlint okm_open(dev_t *devp, int flag, int otyp, struct cred *cred);
25cf1a301a396c38e8adf52c15f537b80d2483f7jlint okm_close(dev_t dev, int flag, int otyp, struct cred *cred);
25cf1a301a396c38e8adf52c15f537b80d2483f7jlint okm_ioctl(dev_t dev, int cmd, intptr_t data, int flag,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Prototypes for the internal functions.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlint okm_get_req(okms_t *okmsp, sckm_ioctl_getreq_t *ireqp,
25cf1a301a396c38e8adf52c15f537b80d2483f7jlint okm_process_req(okms_t *okmsp, okm_req_hdr_t *reqp, uint32_t len,
25cf1a301a396c38e8adf52c15f537b80d2483f7jlint okm_process_status(okms_t *okmsp, sckm_ioctl_status_t *ireply);
25cf1a301a396c38e8adf52c15f537b80d2483f7jlint okm_send_reply(okms_t *okmsp, uint32_t transid, uint32_t status,
25cf1a301a396c38e8adf52c15f537b80d2483f7jlstatic int okm_copyout_ioctl_getreq(sckm_ioctl_getreq_t *driverarg,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl 0, /* streamtab */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl 0, /* refcnt */
193974072f41a843678abf5f61979c748687e66bSherry Moore "OPL Key Management Driver",
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * _init - Module's init routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * _fini - Module's fini routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * _info - Module's info routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_attach - Module's attach routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: Initializes the modules state structure and create
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * the minor device node.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* Only one instance is supported. */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (instance != 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Get an interrupt block cookie corresponding to the
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * interrupt priority of the event handler.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Assert that the event priority is not redefined to
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * some other priority.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* LINTED */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * set clean_node ahead as remove_node has to be called even
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * if create node fails.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_detach - Module's detach routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: Cleans up the module's state structures and any other
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * relevant data.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Check if the mailbox is still in use.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_info - Module's info routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl/* ARGSUSED */
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
6074f19f4f7fc46d66216416827712a7511abffbZach Kissel * We have the case here where the minor number
6074f19f4f7fc46d66216416827712a7511abffbZach Kissel * is the same as the instance number. So, just
6074f19f4f7fc46d66216416827712a7511abffbZach Kissel * make sure we have the right minor node in our
6074f19f4f7fc46d66216416827712a7511abffbZach Kissel * global state. If we don't, set the result to NULL.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_open - Device open routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: Initializes the mailbox and waits until the mailbox
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * gets connected. Only one open at a time is supported.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl/*ARGSUSED*/
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_open(dev_t *devp, int flag, int otyp, struct cred *cred)
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* Only one open supported */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (ret != 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * block_until_ready - Function to wait until the mailbox is ready to use.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: It initializes the mailbox and waits for the mailbox
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * state to transition to connected.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_DRV, ("block_until_ready: closing the mailbox\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_DRV, ("block_until_ready: mailbox connected\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl return (0);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Initialize mailbox.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ("block_until_ready: mailbox init failed ret=%d\n", ret));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_close - Device close routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: Closes the mailbox.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl/*ARGSUSED*/
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_close(dev_t dev, int flag, int otyp, struct cred *cred)
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* Close the lower layer first */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl return (0);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_ioctl - Device ioctl routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: Processes ioctls from the daemon.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl/*ARGSUSED*/
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_ioctl(dev_t dev, int cmd, intptr_t data, int flag, cred_t *cred, int *rvalp)
25cf1a301a396c38e8adf52c15f537b80d2483f7jl switch (cmd) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_get_req - Get a request from the mailbox.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: It blocks until a message is received, then processes
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * the message and returns it to the requestor.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_get_req(okms_t *okmsp, sckm_ioctl_getreq_t *ireqp, intptr_t data, int flag)
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ret = scf_mb_getmsg(okmsp->km_target, okmsp->km_key, len, 1,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl } else if (ret != 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* check message length */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* protocol error, drop message */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* check version of the message received */
07d06da50d310a325b457d6330165aebab1e0064Surya Prakki (void) okm_send_reply(okmsp, reqp->krq_transid,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* process message */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ret = okm_process_req(okmsp, reqp, len, ireqp, data, flag);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * The message is not saved, so free the buffer.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_process_req - Process the request.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: Validate the request and then give the request to the
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_process_req(okms_t *okmsp, okm_req_hdr_t *reqp, uint32_t len,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl void *req_datap = (void *)(((char *)reqp) + sizeof (okm_req_hdr_t));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* sanity check request */
07d06da50d310a325b457d6330165aebab1e0064Surya Prakki (void) okm_send_reply(okmsp, reqp->krq_transid,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Save the message, prior to giving it to the daemon.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ("okm_process_req: not enough space\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (ddi_copyout(req_datap, ireqp->buf, sadb_msglen, flag)) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ("okm_process_req: copyout failed\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ("okm_process_req: copyout failed\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl cmn_err(CE_WARN, "Unknown cmd 0x%x received", reqp->krq_cmd);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Received an unknown command, send corresponding
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * error message.
07d06da50d310a325b457d6330165aebab1e0064Surya Prakki (void) okm_send_reply(okmsp, reqp->krq_transid,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl return (0);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_process_status - Process the status from the daemon.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: Processes the status received from the daemon and sends
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * corresponding message to the SP.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_process_status(okms_t *okmsp, sckm_ioctl_status_t *ireply)
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* fail if no status is expected, or if it does not match */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (!okmsp->km_reqp || (reqp->krq_transid != ireply->transid)) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ("okm_process_status: req/transid mismatch\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_DRV, ("okm_process_status: SADB VERSION ERROR\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_DRV, ("okm_process_status: SADB TYPE ERR\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl cmn_err(CE_WARN, "SCKM daemon returned invalid status %d\n",
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Clean up the cached request now.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (ret == 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_copyin_ioctl_getreq - copy-in the ioctl request from the daemon.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_copyin_ioctl_getreq(intptr_t userarg, sckm_ioctl_getreq_t *driverarg,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl#else /* ! _MULTI_DATAMODEL */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl#endif /* _MULTI_DATAMODEL */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl return (0);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_copyout_ioctl_getreq - copy-out the request to the daemon.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlokm_copyout_ioctl_getreq(sckm_ioctl_getreq_t *driverarg, intptr_t userarg,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl#else /* ! _MULTI_DATAMODEL */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl#endif /* _MULTI_DATAMODEL */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl return (0);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_cleanup - Cleanup routine.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlstatic void
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_mbox_init - Mailbox specific initialization.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* Iterate until mailbox gets connected */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_MBOX, ("okm_mbox_init: mb_init ret=%d\n", ret));
030f3a8fd60560aa3c096f68447cd7ea31457e6araghuram if (ret != 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* Block until the mailbox is ready to communicate. */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* interrupted */
030f3a8fd60560aa3c096f68447cd7ea31457e6araghuram if ((ret != 0) || (okmsp->km_state & OKM_MB_DISC)) {
030f3a8fd60560aa3c096f68447cd7ea31457e6araghuram ("okm_mbox_init: mbox DISC_ERROR\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * If there was failure, then wait for
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * OKM_MB_TOUT secs and retry again.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (ret == 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* if interrupted, return immediately. */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ("okm_mbox_init: interrupted\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * The max msg size should be at least the size of reply
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * we need to send.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if ((ret == 0) && (okmsp->km_maxsz < sizeof (okm_rep_hdr_t))) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl "but found %d\n", sizeof (okm_rep_hdr_t), okmsp->km_maxsz);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (ret != 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_MBOX, ("okm_mbox_init: mb_init ret=%d\n", ret));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_mbox_fini - Mailbox de-initialization.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlstatic void
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_MBOX, ("okm_mbox_fini: mb_fini ret=%d\n", ret));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (ret != 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl okmsp->km_state &= ~(OKM_MB_INITED | OKM_MB_CONN | OKM_MB_DISC);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_event_handler - Mailbox event handler.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Description: Implements a state machine to handle all the mailbox
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * events. For each event, it sets the appropriate state
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * flag and wakes up the threads waiting for that event.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Ignore all events if the state flag indicates that the
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * mailbox not initialized, this may happen during the close.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ("okm_event_handler: event=0x%X - mailbox not inited \n",
25cf1a301a396c38e8adf52c15f537b80d2483f7jl switch (event) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_MBOX, ("okm_event_handler: Event CONN_OK\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Now the mailbox is ready to use, lets wake up
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * any one waiting for this event.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_MBOX, ("okm_event_handler: Event MSG_DATA\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * A message is available in the mailbox,
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * wakeup if any one is ready to read the message.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_MBOX, ("okm_event_handler: Event MB_SPACE\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * Now the mailbox is ready to transmit, lets
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * wakeup if any one is waiting to write.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_MBOX, ("okm_event_handler: Event DISC_ERROR\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_send_reply - Send a mailbox reply message.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl DPRINTF(DBG_MBOX, ("okm_send_reply: putmsg ret=%d\n", ret));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* mailbox is busy, poll/retry */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl /* interrupted */
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ("okm_send_reply: interrupted\n"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * okm_timeout_val -- Return appropriate timeout value.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * A small timeout value is returned for EBUSY as the mailbox busy
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * condition may go away sooner and we are expected to poll.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * A larger timeout value is returned for ENOSPC case, as the condition
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * depends on the peer to release buffer space.
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * NOTE: there will also be an event(SCF_MB_SPACE) but a timeout is
25cf1a301a396c38e8adf52c15f537b80d2483f7jl * used for reliability purposes.
25cf1a301a396c38e8adf52c15f537b80d2483f7jlstatic void
25cf1a301a396c38e8adf52c15f537b80d2483f7jl uint8_t *datap = (uint8_t *)(((char *)reqp) + sizeof (okm_req_hdr_t));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl ((reqp->krq_cmd == OKM_MSG_SADB) ? "MSG_SADB" : "UNKNOWN"));
25cf1a301a396c38e8adf52c15f537b80d2483f7jl for (i = 0; i < msglen; ) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jl for (j = 0; (j < BYTES_PER_LINE) && (i < msglen); j++, i++) {
07d06da50d310a325b457d6330165aebab1e0064Surya Prakki (void) sprintf(&bytestr[j * 3], "%02X ", datap[i]);
25cf1a301a396c38e8adf52c15f537b80d2483f7jl if (j != 0) {
25cf1a301a396c38e8adf52c15f537b80d2483f7jlstatic void
25cf1a301a396c38e8adf52c15f537b80d2483f7jl printf("Sadb_errno=%d Sadb_ver=%d\n", repp->krp_sadb_errno,