10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53448Sdh155122 * Common Development and Distribution License (the "License").
63448Sdh155122 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*11042SErik.Nordmark@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/stream.h>
280Sstevel@tonic-gate #include <sys/sysmacros.h>
290Sstevel@tonic-gate #include <sys/callb.h>
300Sstevel@tonic-gate #include <sys/ddi.h>
310Sstevel@tonic-gate #include <sys/sunddi.h>
320Sstevel@tonic-gate #include <sys/proc.h>
330Sstevel@tonic-gate #include <sys/modctl.h>
340Sstevel@tonic-gate #include <sys/disp.h>
353448Sdh155122 #include <inet/ip.h>
360Sstevel@tonic-gate #include <inet/ipsec_impl.h>
373448Sdh155122 #include <inet/optcom.h>
383448Sdh155122 #include <inet/keysock.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
413448Sdh155122 * Loader commands for ipsec_loader_sig
420Sstevel@tonic-gate */
430Sstevel@tonic-gate #define IPSEC_LOADER_EXITNOW -1
440Sstevel@tonic-gate #define IPSEC_LOADER_LOADNOW 1
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * NOTE: This function is entered w/o holding any STREAMS perimeters.
480Sstevel@tonic-gate */
490Sstevel@tonic-gate static void
ipsec_loader(void * arg)503448Sdh155122 ipsec_loader(void *arg)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate callb_cpr_t cprinfo;
530Sstevel@tonic-gate boolean_t ipsec_failure = B_FALSE;
543448Sdh155122 ipsec_stack_t *ipss = (ipsec_stack_t *)arg;
550Sstevel@tonic-gate
563448Sdh155122 CALLB_CPR_INIT(&cprinfo, &ipss->ipsec_loader_lock, callb_generic_cpr,
570Sstevel@tonic-gate "ipsec_loader");
583448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock);
590Sstevel@tonic-gate for (;;) {
600Sstevel@tonic-gate
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate * Wait for someone to tell me to continue.
630Sstevel@tonic-gate */
643448Sdh155122 while (ipss->ipsec_loader_sig == IPSEC_LOADER_WAIT) {
650Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo);
663448Sdh155122 cv_wait(&ipss->ipsec_loader_sig_cv,
673448Sdh155122 &ipss->ipsec_loader_lock);
683448Sdh155122 CALLB_CPR_SAFE_END(&cprinfo, &ipss->ipsec_loader_lock);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate
710Sstevel@tonic-gate /* IPSEC_LOADER_EXITNOW implies signal by _fini(). */
723448Sdh155122 if (ipss->ipsec_loader_sig == IPSEC_LOADER_EXITNOW) {
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate * Let user patch ipsec_loader_tid to
750Sstevel@tonic-gate * 0 to try again.
760Sstevel@tonic-gate */
773448Sdh155122 ipss->ipsec_loader_state = IPSEC_LOADER_FAILED;
783448Sdh155122 ipss->ipsec_loader_sig = IPSEC_LOADER_WAIT;
790Sstevel@tonic-gate
800Sstevel@tonic-gate /* ipsec_loader_lock is held at this point! */
813448Sdh155122 ASSERT(MUTEX_HELD(&ipss->ipsec_loader_lock));
820Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo);
837373Ssommerfeld@sun.com ASSERT(MUTEX_NOT_HELD(&ipss->ipsec_loader_lock));
840Sstevel@tonic-gate thread_exit();
850Sstevel@tonic-gate }
863448Sdh155122 mutex_exit(&ipss->ipsec_loader_lock);
870Sstevel@tonic-gate
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate * Load IPsec, which is done by modloading keysock and calling
900Sstevel@tonic-gate * keysock_plumb_ipsec().
910Sstevel@tonic-gate */
920Sstevel@tonic-gate
930Sstevel@tonic-gate /* Pardon my hardcoding... */
940Sstevel@tonic-gate if (modload("drv", "keysock") == -1) {
950Sstevel@tonic-gate cmn_err(CE_WARN, "IP: Cannot load keysock.");
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate * Only this function can set ipsec_failure. If the
980Sstevel@tonic-gate * damage can be repaired, use adb to set this to
990Sstevel@tonic-gate * B_FALSE and try again.
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate ipsec_failure = B_TRUE;
1023448Sdh155122 } else if (keysock_plumb_ipsec(ipss->ipsec_netstack) != 0) {
1030Sstevel@tonic-gate cmn_err(CE_WARN, "IP: Cannot plumb IPsec.");
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate * Only this function can set ipsec_failure. If the
1060Sstevel@tonic-gate * damage can be repaired, use adb to set this to
1070Sstevel@tonic-gate * B_FALSE and try again.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate ipsec_failure = B_TRUE;
1100Sstevel@tonic-gate } else {
1110Sstevel@tonic-gate ipsec_failure = B_FALSE;
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1143448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock);
1150Sstevel@tonic-gate if (ipsec_failure) {
1163448Sdh155122 if (ipss->ipsec_loader_sig == IPSEC_LOADER_LOADNOW)
1173448Sdh155122 ipss->ipsec_loader_sig = IPSEC_LOADER_WAIT;
1183448Sdh155122 ipss->ipsec_loader_state = IPSEC_LOADER_FAILED;
1190Sstevel@tonic-gate } else {
1203448Sdh155122 ipss->ipsec_loader_state = IPSEC_LOADER_SUCCEEDED;
1210Sstevel@tonic-gate }
1223448Sdh155122 mutex_exit(&ipss->ipsec_loader_lock);
1230Sstevel@tonic-gate
1243448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock);
1250Sstevel@tonic-gate if (!ipsec_failure) {
1260Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo);
1277373Ssommerfeld@sun.com ASSERT(MUTEX_NOT_HELD(&ipss->ipsec_loader_lock));
1280Sstevel@tonic-gate ipsec_register_prov_update();
1290Sstevel@tonic-gate thread_exit();
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate * Called from ip_ddi_init() to initialize ipsec loader thread.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate void
ipsec_loader_init(ipsec_stack_t * ipss)1383448Sdh155122 ipsec_loader_init(ipsec_stack_t *ipss)
1390Sstevel@tonic-gate {
1403448Sdh155122 mutex_init(&ipss->ipsec_loader_lock, NULL, MUTEX_DEFAULT, NULL);
1413448Sdh155122 cv_init(&ipss->ipsec_loader_sig_cv, NULL, CV_DEFAULT, NULL);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate /*
1450Sstevel@tonic-gate * Called from ip_ddi_destroy() to take down ipsec loader thread.
1460Sstevel@tonic-gate */
1470Sstevel@tonic-gate void
ipsec_loader_destroy(ipsec_stack_t * ipss)1483448Sdh155122 ipsec_loader_destroy(ipsec_stack_t *ipss)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate kt_did_t tid;
1510Sstevel@tonic-gate
1523448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock);
1533448Sdh155122 tid = ipss->ipsec_loader_tid;
1540Sstevel@tonic-gate if (tid != 0) {
1553448Sdh155122 ipss->ipsec_loader_sig = IPSEC_LOADER_EXITNOW;
1563448Sdh155122 cv_signal(&ipss->ipsec_loader_sig_cv);
1573448Sdh155122 ipss->ipsec_loader_tid = 0;
1580Sstevel@tonic-gate }
1593448Sdh155122 mutex_exit(&ipss->ipsec_loader_lock);
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate /*
1620Sstevel@tonic-gate * Wait for ipsec_loader() to finish before we destroy
1630Sstevel@tonic-gate * cvs and mutexes.
1640Sstevel@tonic-gate */
1650Sstevel@tonic-gate if (tid != 0)
1660Sstevel@tonic-gate thread_join(tid);
1670Sstevel@tonic-gate
1683448Sdh155122 mutex_destroy(&ipss->ipsec_loader_lock);
1693448Sdh155122 cv_destroy(&ipss->ipsec_loader_sig_cv);
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate void
ipsec_loader_start(ipsec_stack_t * ipss)1733448Sdh155122 ipsec_loader_start(ipsec_stack_t *ipss)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate kthread_t *tp;
1760Sstevel@tonic-gate
1773448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock);
1780Sstevel@tonic-gate
1793448Sdh155122 if (ipss->ipsec_loader_tid == 0) {
1803448Sdh155122 tp = thread_create(NULL, 0, ipsec_loader, ipss, 0, &p0,
1810Sstevel@tonic-gate TS_RUN, MAXCLSYSPRI);
1823448Sdh155122 ipss->ipsec_loader_tid = tp->t_did;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate /* Else we lost the race, oh well. */
1853448Sdh155122 mutex_exit(&ipss->ipsec_loader_lock);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate void
ipsec_loader_loadnow(ipsec_stack_t * ipss)1893448Sdh155122 ipsec_loader_loadnow(ipsec_stack_t *ipss)
1900Sstevel@tonic-gate {
1910Sstevel@tonic-gate /*
1920Sstevel@tonic-gate * It is possible that an algorithm update message was
1930Sstevel@tonic-gate * received before IPsec is loaded. Such messages are
1940Sstevel@tonic-gate * saved in spdsock for later processing. Since IPsec
1950Sstevel@tonic-gate * loading can be initiated by interfaces different
1960Sstevel@tonic-gate * than spdsock, we must trigger the processing of
1970Sstevel@tonic-gate * update messages from the ipsec loader.
1980Sstevel@tonic-gate */
1993448Sdh155122 spdsock_update_pending_algs(ipss->ipsec_netstack);
2000Sstevel@tonic-gate
2013448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock);
2023448Sdh155122 if ((ipss->ipsec_loader_state == IPSEC_LOADER_WAIT) &&
2033448Sdh155122 (ipss->ipsec_loader_sig == IPSEC_LOADER_WAIT)) {
2043448Sdh155122 ipss->ipsec_loader_sig = IPSEC_LOADER_LOADNOW;
2053448Sdh155122 cv_signal(&ipss->ipsec_loader_sig_cv);
2060Sstevel@tonic-gate }
2073448Sdh155122 mutex_exit(&ipss->ipsec_loader_lock);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate * Dummy callback routine (placeholder) to avoid keysock plumbing
2120Sstevel@tonic-gate * races. Used in conjunction with qtimeout() and qwait() to wait
2130Sstevel@tonic-gate * until ipsec has loaded -- the qwait() in ipsec_loader_loadwait will
2140Sstevel@tonic-gate * wake up once this routine returns.
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate /* ARGSUSED */
2180Sstevel@tonic-gate static void
loader_nop(void * ignoreme)2190Sstevel@tonic-gate loader_nop(void *ignoreme)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate * Called from keysock driver open to delay until ipsec is done loading.
2250Sstevel@tonic-gate * Returns B_TRUE if it worked, B_FALSE if it didn't.
2260Sstevel@tonic-gate */
2270Sstevel@tonic-gate boolean_t
ipsec_loader_wait(queue_t * q,ipsec_stack_t * ipss)2283448Sdh155122 ipsec_loader_wait(queue_t *q, ipsec_stack_t *ipss)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate * 30ms delay per loop is arbitrary; it takes ~300ms to
2320Sstevel@tonic-gate * load and plumb ipsec on an ultra-1.
2330Sstevel@tonic-gate */
2340Sstevel@tonic-gate
2353448Sdh155122 while (ipss->ipsec_loader_state == IPSEC_LOADER_WAIT) {
2360Sstevel@tonic-gate (void) qtimeout(q, loader_nop, 0, drv_usectohz(30000));
2370Sstevel@tonic-gate qwait(q);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2403448Sdh155122 return (ipss->ipsec_loader_state == IPSEC_LOADER_SUCCEEDED);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate * Just check to see if IPsec is loaded (or not).
2450Sstevel@tonic-gate */
2460Sstevel@tonic-gate boolean_t
ipsec_loaded(ipsec_stack_t * ipss)2473448Sdh155122 ipsec_loaded(ipsec_stack_t *ipss)
2480Sstevel@tonic-gate {
2493448Sdh155122 return (ipss->ipsec_loader_state == IPSEC_LOADER_SUCCEEDED);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate * Check to see if IPsec loading failed.
2540Sstevel@tonic-gate */
2550Sstevel@tonic-gate boolean_t
ipsec_failed(ipsec_stack_t * ipss)2563448Sdh155122 ipsec_failed(ipsec_stack_t *ipss)
2570Sstevel@tonic-gate {
2583448Sdh155122 return (ipss->ipsec_loader_state == IPSEC_LOADER_FAILED);
2590Sstevel@tonic-gate }
260