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*7373Ssommerfeld@sun.com * Copyright 2008 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 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); 83*7373Ssommerfeld@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 ip_ipsec_load_complete(ipss); 1250Sstevel@tonic-gate 1263448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock); 1270Sstevel@tonic-gate if (!ipsec_failure) { 1280Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 129*7373Ssommerfeld@sun.com ASSERT(MUTEX_NOT_HELD(&ipss->ipsec_loader_lock)); 1300Sstevel@tonic-gate ipsec_register_prov_update(); 1310Sstevel@tonic-gate thread_exit(); 1320Sstevel@tonic-gate } 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * Called from ip_ddi_init() to initialize ipsec loader thread. 1380Sstevel@tonic-gate */ 1390Sstevel@tonic-gate void 1403448Sdh155122 ipsec_loader_init(ipsec_stack_t *ipss) 1410Sstevel@tonic-gate { 1423448Sdh155122 mutex_init(&ipss->ipsec_loader_lock, NULL, MUTEX_DEFAULT, NULL); 1433448Sdh155122 cv_init(&ipss->ipsec_loader_sig_cv, NULL, CV_DEFAULT, NULL); 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate /* 1470Sstevel@tonic-gate * Called from ip_ddi_destroy() to take down ipsec loader thread. 1480Sstevel@tonic-gate */ 1490Sstevel@tonic-gate void 1503448Sdh155122 ipsec_loader_destroy(ipsec_stack_t *ipss) 1510Sstevel@tonic-gate { 1520Sstevel@tonic-gate kt_did_t tid; 1530Sstevel@tonic-gate 1543448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock); 1553448Sdh155122 tid = ipss->ipsec_loader_tid; 1560Sstevel@tonic-gate if (tid != 0) { 1573448Sdh155122 ipss->ipsec_loader_sig = IPSEC_LOADER_EXITNOW; 1583448Sdh155122 cv_signal(&ipss->ipsec_loader_sig_cv); 1593448Sdh155122 ipss->ipsec_loader_tid = 0; 1600Sstevel@tonic-gate } 1613448Sdh155122 mutex_exit(&ipss->ipsec_loader_lock); 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate /* 1640Sstevel@tonic-gate * Wait for ipsec_loader() to finish before we destroy 1650Sstevel@tonic-gate * cvs and mutexes. 1660Sstevel@tonic-gate */ 1670Sstevel@tonic-gate if (tid != 0) 1680Sstevel@tonic-gate thread_join(tid); 1690Sstevel@tonic-gate 1703448Sdh155122 mutex_destroy(&ipss->ipsec_loader_lock); 1713448Sdh155122 cv_destroy(&ipss->ipsec_loader_sig_cv); 1720Sstevel@tonic-gate } 1730Sstevel@tonic-gate 1740Sstevel@tonic-gate void 1753448Sdh155122 ipsec_loader_start(ipsec_stack_t *ipss) 1760Sstevel@tonic-gate { 1770Sstevel@tonic-gate kthread_t *tp; 1780Sstevel@tonic-gate 1793448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock); 1800Sstevel@tonic-gate 1813448Sdh155122 if (ipss->ipsec_loader_tid == 0) { 1823448Sdh155122 tp = thread_create(NULL, 0, ipsec_loader, ipss, 0, &p0, 1830Sstevel@tonic-gate TS_RUN, MAXCLSYSPRI); 1843448Sdh155122 ipss->ipsec_loader_tid = tp->t_did; 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate /* Else we lost the race, oh well. */ 1873448Sdh155122 mutex_exit(&ipss->ipsec_loader_lock); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate void 1913448Sdh155122 ipsec_loader_loadnow(ipsec_stack_t *ipss) 1920Sstevel@tonic-gate { 1930Sstevel@tonic-gate /* 1940Sstevel@tonic-gate * It is possible that an algorithm update message was 1950Sstevel@tonic-gate * received before IPsec is loaded. Such messages are 1960Sstevel@tonic-gate * saved in spdsock for later processing. Since IPsec 1970Sstevel@tonic-gate * loading can be initiated by interfaces different 1980Sstevel@tonic-gate * than spdsock, we must trigger the processing of 1990Sstevel@tonic-gate * update messages from the ipsec loader. 2000Sstevel@tonic-gate */ 2013448Sdh155122 spdsock_update_pending_algs(ipss->ipsec_netstack); 2020Sstevel@tonic-gate 2033448Sdh155122 mutex_enter(&ipss->ipsec_loader_lock); 2043448Sdh155122 if ((ipss->ipsec_loader_state == IPSEC_LOADER_WAIT) && 2053448Sdh155122 (ipss->ipsec_loader_sig == IPSEC_LOADER_WAIT)) { 2063448Sdh155122 ipss->ipsec_loader_sig = IPSEC_LOADER_LOADNOW; 2073448Sdh155122 cv_signal(&ipss->ipsec_loader_sig_cv); 2080Sstevel@tonic-gate } 2093448Sdh155122 mutex_exit(&ipss->ipsec_loader_lock); 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate 2120Sstevel@tonic-gate /* 2130Sstevel@tonic-gate * Dummy callback routine (placeholder) to avoid keysock plumbing 2140Sstevel@tonic-gate * races. Used in conjunction with qtimeout() and qwait() to wait 2150Sstevel@tonic-gate * until ipsec has loaded -- the qwait() in ipsec_loader_loadwait will 2160Sstevel@tonic-gate * wake up once this routine returns. 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* ARGSUSED */ 2200Sstevel@tonic-gate static void 2210Sstevel@tonic-gate loader_nop(void *ignoreme) 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate } 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* 2260Sstevel@tonic-gate * Called from keysock driver open to delay until ipsec is done loading. 2270Sstevel@tonic-gate * Returns B_TRUE if it worked, B_FALSE if it didn't. 2280Sstevel@tonic-gate */ 2290Sstevel@tonic-gate boolean_t 2303448Sdh155122 ipsec_loader_wait(queue_t *q, ipsec_stack_t *ipss) 2310Sstevel@tonic-gate { 2320Sstevel@tonic-gate /* 2330Sstevel@tonic-gate * 30ms delay per loop is arbitrary; it takes ~300ms to 2340Sstevel@tonic-gate * load and plumb ipsec on an ultra-1. 2350Sstevel@tonic-gate */ 2360Sstevel@tonic-gate 2373448Sdh155122 while (ipss->ipsec_loader_state == IPSEC_LOADER_WAIT) { 2380Sstevel@tonic-gate (void) qtimeout(q, loader_nop, 0, drv_usectohz(30000)); 2390Sstevel@tonic-gate qwait(q); 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2423448Sdh155122 return (ipss->ipsec_loader_state == IPSEC_LOADER_SUCCEEDED); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* 2460Sstevel@tonic-gate * Just check to see if IPsec is loaded (or not). 2470Sstevel@tonic-gate */ 2480Sstevel@tonic-gate boolean_t 2493448Sdh155122 ipsec_loaded(ipsec_stack_t *ipss) 2500Sstevel@tonic-gate { 2513448Sdh155122 return (ipss->ipsec_loader_state == IPSEC_LOADER_SUCCEEDED); 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate /* 2550Sstevel@tonic-gate * Check to see if IPsec loading failed. 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate boolean_t 2583448Sdh155122 ipsec_failed(ipsec_stack_t *ipss) 2590Sstevel@tonic-gate { 2603448Sdh155122 return (ipss->ipsec_loader_state == IPSEC_LOADER_FAILED); 2610Sstevel@tonic-gate } 262