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 5*2465Sdanmcd * Common Development and Distribution License (the "License"). 6*2465Sdanmcd * 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*2465Sdanmcd * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/param.h> 300Sstevel@tonic-gate #include <sys/types.h> 310Sstevel@tonic-gate #include <sys/stream.h> 320Sstevel@tonic-gate #include <sys/strsubr.h> 330Sstevel@tonic-gate #include <sys/strsun.h> 340Sstevel@tonic-gate #include <sys/stropts.h> 350Sstevel@tonic-gate #include <sys/vnode.h> 360Sstevel@tonic-gate #include <sys/strlog.h> 370Sstevel@tonic-gate #include <sys/sysmacros.h> 380Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 390Sstevel@tonic-gate #include <sys/tihdr.h> 400Sstevel@tonic-gate #include <sys/timod.h> 410Sstevel@tonic-gate #include <sys/tiuser.h> 420Sstevel@tonic-gate #include <sys/ddi.h> 430Sstevel@tonic-gate #include <sys/sunddi.h> 440Sstevel@tonic-gate #include <sys/sunldi.h> 450Sstevel@tonic-gate #include <sys/file.h> 460Sstevel@tonic-gate #include <sys/modctl.h> 470Sstevel@tonic-gate #include <sys/debug.h> 480Sstevel@tonic-gate #include <sys/kmem.h> 490Sstevel@tonic-gate #include <sys/cmn_err.h> 500Sstevel@tonic-gate #include <sys/proc.h> 510Sstevel@tonic-gate #include <sys/suntpi.h> 520Sstevel@tonic-gate #include <sys/atomic.h> 530Sstevel@tonic-gate #include <sys/mkdev.h> 540Sstevel@tonic-gate #include <sys/policy.h> 550Sstevel@tonic-gate 560Sstevel@tonic-gate #include <sys/socket.h> 570Sstevel@tonic-gate #include <netinet/in.h> 580Sstevel@tonic-gate #include <net/pfkeyv2.h> 590Sstevel@tonic-gate 600Sstevel@tonic-gate #include <inet/common.h> 610Sstevel@tonic-gate #include <netinet/ip6.h> 620Sstevel@tonic-gate #include <inet/ip.h> 630Sstevel@tonic-gate #include <inet/mi.h> 640Sstevel@tonic-gate #include <inet/nd.h> 650Sstevel@tonic-gate #include <inet/optcom.h> 660Sstevel@tonic-gate #include <inet/ipsec_info.h> 670Sstevel@tonic-gate #include <inet/ipsec_impl.h> 680Sstevel@tonic-gate #include <inet/keysock.h> 690Sstevel@tonic-gate 700Sstevel@tonic-gate #include <sys/isa_defs.h> 710Sstevel@tonic-gate 720Sstevel@tonic-gate /* 730Sstevel@tonic-gate * This is a transport provider for the PF_KEY key mangement socket. 740Sstevel@tonic-gate * (See RFC 2367 for details.) 750Sstevel@tonic-gate * Downstream messages are wrapped in a keysock consumer interface KEYSOCK_IN 760Sstevel@tonic-gate * messages (see ipsec_info.h), and passed to the appropriate consumer. 770Sstevel@tonic-gate * Upstream messages are generated for all open PF_KEY sockets, when 780Sstevel@tonic-gate * appropriate, as well as the sender (as long as SO_USELOOPBACK is enabled) 790Sstevel@tonic-gate * in reply to downstream messages. 800Sstevel@tonic-gate * 810Sstevel@tonic-gate * Upstream messages must be created asynchronously for the following 820Sstevel@tonic-gate * situations: 830Sstevel@tonic-gate * 840Sstevel@tonic-gate * 1.) A keysock consumer requires an SA, and there is currently none. 850Sstevel@tonic-gate * 2.) An SA expires, either hard or soft lifetime. 860Sstevel@tonic-gate * 3.) Other events a consumer deems fit. 870Sstevel@tonic-gate * 880Sstevel@tonic-gate * The MT model of this is PERMOD, with shared put procedures. Two types of 890Sstevel@tonic-gate * messages, SADB_FLUSH and SADB_DUMP, need to lock down the perimeter to send 900Sstevel@tonic-gate * down the *multiple* messages they create. 910Sstevel@tonic-gate */ 920Sstevel@tonic-gate 930Sstevel@tonic-gate /* List of open PF_KEY sockets, protected by keysock_list_lock. */ 940Sstevel@tonic-gate static kmutex_t keysock_list_lock; 950Sstevel@tonic-gate static keysock_t *keysock_list; 960Sstevel@tonic-gate 970Sstevel@tonic-gate static vmem_t *keysock_vmem; /* for minor numbers. */ 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* Consumers table. If an entry is NULL, keysock maintains the table. */ 1000Sstevel@tonic-gate static kmutex_t keysock_consumers_lock; 1010Sstevel@tonic-gate 1020Sstevel@tonic-gate #define KEYSOCK_MAX_CONSUMERS 256 1030Sstevel@tonic-gate static keysock_consumer_t *keysock_consumers[KEYSOCK_MAX_CONSUMERS]; 1040Sstevel@tonic-gate 1050Sstevel@tonic-gate /* Default structure copied into T_INFO_ACK messages (from rts.c...) */ 1060Sstevel@tonic-gate static struct T_info_ack keysock_g_t_info_ack = { 1070Sstevel@tonic-gate T_INFO_ACK, 1080Sstevel@tonic-gate T_INFINITE, /* TSDU_size. Maximum size messages. */ 1090Sstevel@tonic-gate T_INVALID, /* ETSDU_size. No expedited data. */ 1100Sstevel@tonic-gate T_INVALID, /* CDATA_size. No connect data. */ 1110Sstevel@tonic-gate T_INVALID, /* DDATA_size. No disconnect data. */ 1120Sstevel@tonic-gate 0, /* ADDR_size. */ 1130Sstevel@tonic-gate 0, /* OPT_size. No user-settable options */ 1140Sstevel@tonic-gate 64 * 1024, /* TIDU_size. keysock allows maximum size messages. */ 1150Sstevel@tonic-gate T_COTS, /* SERV_type. keysock supports connection oriented. */ 1160Sstevel@tonic-gate TS_UNBND, /* CURRENT_state. This is set from keysock_state. */ 1170Sstevel@tonic-gate (XPG4_1) /* Provider flags */ 1180Sstevel@tonic-gate }; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */ 1210Sstevel@tonic-gate typedef struct keysockpparam_s { 1220Sstevel@tonic-gate uint_t keysock_param_min; 1230Sstevel@tonic-gate uint_t keysock_param_max; 1240Sstevel@tonic-gate uint_t keysock_param_value; 1250Sstevel@tonic-gate char *keysock_param_name; 1260Sstevel@tonic-gate } keysockparam_t; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate /* 1290Sstevel@tonic-gate * Table of NDD variables supported by keysock. These are loaded into 1300Sstevel@tonic-gate * keysock_g_nd in keysock_init_nd. 1310Sstevel@tonic-gate * All of these are alterable, within the min/max values given, at run time. 1320Sstevel@tonic-gate */ 1330Sstevel@tonic-gate static keysockparam_t keysock_param_arr[] = { 1340Sstevel@tonic-gate /* min max value name */ 1350Sstevel@tonic-gate { 4096, 65536, 8192, "keysock_xmit_hiwat"}, 1360Sstevel@tonic-gate { 0, 65536, 1024, "keysock_xmit_lowat"}, 1370Sstevel@tonic-gate { 4096, 65536, 8192, "keysock_recv_hiwat"}, 1380Sstevel@tonic-gate { 65536, 1024*1024*1024, 256*1024, "keysock_max_buf"}, 1390Sstevel@tonic-gate { 0, 3, 0, "keysock_debug"}, 1400Sstevel@tonic-gate }; 1410Sstevel@tonic-gate #define keysock_xmit_hiwat keysock_param_arr[0].keysock_param_value 1420Sstevel@tonic-gate #define keysock_xmit_lowat keysock_param_arr[1].keysock_param_value 1430Sstevel@tonic-gate #define keysock_recv_hiwat keysock_param_arr[2].keysock_param_value 1440Sstevel@tonic-gate #define keysock_max_buf keysock_param_arr[3].keysock_param_value 1450Sstevel@tonic-gate #define keysock_debug keysock_param_arr[4].keysock_param_value 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate kmutex_t keysock_param_lock; /* Protects the NDD variables. */ 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate #define ks0dbg(a) printf a 1500Sstevel@tonic-gate /* NOTE: != 0 instead of > 0 so lint doesn't complain. */ 1510Sstevel@tonic-gate #define ks1dbg(a) if (keysock_debug != 0) printf a 1520Sstevel@tonic-gate #define ks2dbg(a) if (keysock_debug > 1) printf a 1530Sstevel@tonic-gate #define ks3dbg(a) if (keysock_debug > 2) printf a 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate static IDP keysock_g_nd; 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate /* 1580Sstevel@tonic-gate * State for flush/dump. This would normally be a boolean_t, but 1590Sstevel@tonic-gate * cas32() works best for a known 32-bit quantity. 1600Sstevel@tonic-gate */ 1610Sstevel@tonic-gate static uint32_t keysock_flushdump; 1620Sstevel@tonic-gate static int keysock_flushdump_errno; 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate static int keysock_close(queue_t *); 1650Sstevel@tonic-gate static int keysock_open(queue_t *, dev_t *, int, int, cred_t *); 1660Sstevel@tonic-gate static void keysock_wput(queue_t *, mblk_t *); 1670Sstevel@tonic-gate static void keysock_rput(queue_t *, mblk_t *); 1680Sstevel@tonic-gate static void keysock_rsrv(queue_t *); 1690Sstevel@tonic-gate static void keysock_passup(mblk_t *, sadb_msg_t *, minor_t, 1700Sstevel@tonic-gate keysock_consumer_t *, boolean_t); 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate static struct module_info info = { 1730Sstevel@tonic-gate 5138, "keysock", 1, INFPSZ, 512, 128 1740Sstevel@tonic-gate }; 1750Sstevel@tonic-gate 1760Sstevel@tonic-gate static struct qinit rinit = { 1770Sstevel@tonic-gate (pfi_t)keysock_rput, (pfi_t)keysock_rsrv, keysock_open, keysock_close, 1780Sstevel@tonic-gate NULL, &info 1790Sstevel@tonic-gate }; 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate static struct qinit winit = { 1820Sstevel@tonic-gate (pfi_t)keysock_wput, NULL, NULL, NULL, NULL, &info 1830Sstevel@tonic-gate }; 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate struct streamtab keysockinfo = { 1860Sstevel@tonic-gate &rinit, &winit 1870Sstevel@tonic-gate }; 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate extern struct modlinkage *keysock_modlp; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate /* 1920Sstevel@tonic-gate * Plumb IPsec. 1930Sstevel@tonic-gate * 1940Sstevel@tonic-gate * NOTE: New "default" modules will need to be loaded here if needed before 1950Sstevel@tonic-gate * boot time. 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate /* Keep these in global space to keep the lint from complaining. */ 1990Sstevel@tonic-gate static char *IPSECESP = "ipsecesp"; 2000Sstevel@tonic-gate static char *IPSECESPDEV = "/devices/pseudo/ipsecesp@0:ipsecesp"; 2010Sstevel@tonic-gate static char *IPSECAH = "ipsecah"; 2020Sstevel@tonic-gate static char *IPSECAHDEV = "/devices/pseudo/ipsecah@0:ipsecah"; 2030Sstevel@tonic-gate static char *IP6DEV = "/devices/pseudo/ip6@0:ip6"; 2040Sstevel@tonic-gate static char *KEYSOCK = "keysock"; 2050Sstevel@tonic-gate static char *STRMOD = "strmod"; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate /* 2080Sstevel@tonic-gate * keysock_plumbed: zero if plumb not attempted, positive if it succeeded, 2090Sstevel@tonic-gate * negative if it failed. 2100Sstevel@tonic-gate */ 2110Sstevel@tonic-gate static int keysock_plumbed = 0; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate /* 2140Sstevel@tonic-gate * This integer counts the number of extended REGISTERed sockets. This 2150Sstevel@tonic-gate * determines if we should send extended REGISTERs. 2160Sstevel@tonic-gate */ 2170Sstevel@tonic-gate static uint32_t keysock_num_extended = 0; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate /* 2200Sstevel@tonic-gate * Global sequence space for SADB_ACQUIRE messages of any sort. 2210Sstevel@tonic-gate */ 2220Sstevel@tonic-gate static uint32_t keysock_acquire_seq = 0xffffffff; 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* 2250Sstevel@tonic-gate * Load the other ipsec modules and plumb them together. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate int 2280Sstevel@tonic-gate keysock_plumb_ipsec(void) 2290Sstevel@tonic-gate { 2300Sstevel@tonic-gate ldi_handle_t lh, ip6_lh = NULL; 2310Sstevel@tonic-gate ldi_ident_t li = NULL; 2320Sstevel@tonic-gate int err = 0; 2330Sstevel@tonic-gate int muxid, rval; 2340Sstevel@tonic-gate boolean_t esp_present = B_TRUE; 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate keysock_plumbed = 0; /* we're trying again.. */ 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* 2400Sstevel@tonic-gate * Load up the drivers (AH/ESP). 2410Sstevel@tonic-gate * 2420Sstevel@tonic-gate * I do this separately from the actual plumbing in case this function 2430Sstevel@tonic-gate * ever gets called from a diskless boot before the root filesystem is 2440Sstevel@tonic-gate * up. I don't have to worry about "keysock" because, well, if I'm 2450Sstevel@tonic-gate * here, keysock must've loaded successfully. 2460Sstevel@tonic-gate */ 2470Sstevel@tonic-gate if (i_ddi_attach_pseudo_node(IPSECAH) == NULL) { 2480Sstevel@tonic-gate ks0dbg(("IPsec: AH failed to attach.\n")); 2490Sstevel@tonic-gate goto bail; 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate if (i_ddi_attach_pseudo_node(IPSECESP) == NULL) { 2520Sstevel@tonic-gate ks0dbg(("IPsec: ESP failed to attach.\n")); 2530Sstevel@tonic-gate esp_present = B_FALSE; 2540Sstevel@tonic-gate } 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate /* 2570Sstevel@tonic-gate * Set up the IP streams for AH and ESP, as well as tacking keysock 2580Sstevel@tonic-gate * on top of them. Assume keysock has set the autopushes up already. 2590Sstevel@tonic-gate */ 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate /* Open IP. */ 2620Sstevel@tonic-gate err = ldi_ident_from_mod(keysock_modlp, &li); 2630Sstevel@tonic-gate if (err) { 2640Sstevel@tonic-gate ks0dbg(("IPsec: lid_ident_from_mod failed (err %d).\n", 2650Sstevel@tonic-gate err)); 2660Sstevel@tonic-gate goto bail; 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate err = ldi_open_by_name(IP6DEV, FREAD|FWRITE, CRED(), &ip6_lh, li); 2700Sstevel@tonic-gate if (err) { 2710Sstevel@tonic-gate ks0dbg(("IPsec: Open of IP6 failed (err %d).\n", err)); 2720Sstevel@tonic-gate goto bail; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate 2750Sstevel@tonic-gate /* PLINK KEYSOCK/AH */ 2760Sstevel@tonic-gate err = ldi_open_by_name(IPSECAHDEV, FREAD|FWRITE, CRED(), &lh, li); 2770Sstevel@tonic-gate if (err) { 2780Sstevel@tonic-gate ks0dbg(("IPsec: Open of AH failed (err %d).\n", err)); 2790Sstevel@tonic-gate goto bail; 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate err = ldi_ioctl(lh, 2820Sstevel@tonic-gate I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, CRED(), &rval); 2830Sstevel@tonic-gate if (err) { 2840Sstevel@tonic-gate ks0dbg(("IPsec: Push of KEYSOCK onto AH failed (err %d).\n", 2850Sstevel@tonic-gate err)); 2860Sstevel@tonic-gate (void) ldi_close(lh, FREAD|FWRITE, CRED()); 2870Sstevel@tonic-gate goto bail; 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh, 2900Sstevel@tonic-gate FREAD+FWRITE+FNOCTTY+FKIOCTL, kcred, &muxid); 2910Sstevel@tonic-gate if (err) { 2920Sstevel@tonic-gate ks0dbg(("IPsec: PLINK of KEYSOCK/AH failed (err %d).\n", err)); 2930Sstevel@tonic-gate (void) ldi_close(lh, FREAD|FWRITE, CRED()); 2940Sstevel@tonic-gate goto bail; 2950Sstevel@tonic-gate } 2960Sstevel@tonic-gate (void) ldi_close(lh, FREAD|FWRITE, CRED()); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate /* PLINK KEYSOCK/ESP */ 2990Sstevel@tonic-gate if (esp_present) { 3000Sstevel@tonic-gate err = ldi_open_by_name(IPSECESPDEV, 3010Sstevel@tonic-gate FREAD|FWRITE, CRED(), &lh, li); 3020Sstevel@tonic-gate if (err) { 3030Sstevel@tonic-gate ks0dbg(("IPsec: Open of ESP failed (err %d).\n", err)); 3040Sstevel@tonic-gate goto bail; 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate err = ldi_ioctl(lh, 3070Sstevel@tonic-gate I_PUSH, (intptr_t)KEYSOCK, FKIOCTL, CRED(), &rval); 3080Sstevel@tonic-gate if (err) { 3090Sstevel@tonic-gate ks0dbg(("IPsec: " 3100Sstevel@tonic-gate "Push of KEYSOCK onto ESP failed (err %d).\n", 3110Sstevel@tonic-gate err)); 3120Sstevel@tonic-gate (void) ldi_close(lh, FREAD|FWRITE, CRED()); 3130Sstevel@tonic-gate goto bail; 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate err = ldi_ioctl(ip6_lh, I_PLINK, (intptr_t)lh, 3160Sstevel@tonic-gate FREAD+FWRITE+FNOCTTY+FKIOCTL, kcred, &muxid); 3170Sstevel@tonic-gate if (err) { 3180Sstevel@tonic-gate ks0dbg(("IPsec: " 3190Sstevel@tonic-gate "PLINK of KEYSOCK/ESP failed (err %d).\n", err)); 3200Sstevel@tonic-gate (void) ldi_close(lh, FREAD|FWRITE, CRED()); 3210Sstevel@tonic-gate goto bail; 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate (void) ldi_close(lh, FREAD|FWRITE, CRED()); 3240Sstevel@tonic-gate } 3250Sstevel@tonic-gate 3260Sstevel@tonic-gate bail: 3270Sstevel@tonic-gate keysock_plumbed = (err == 0) ? 1 : -1; 3280Sstevel@tonic-gate if (ip6_lh != NULL) { 3290Sstevel@tonic-gate (void) ldi_close(ip6_lh, FREAD|FWRITE, CRED()); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate if (li != NULL) 3320Sstevel@tonic-gate ldi_ident_release(li); 3330Sstevel@tonic-gate return (err); 3340Sstevel@tonic-gate } 3350Sstevel@tonic-gate 3360Sstevel@tonic-gate /* ARGSUSED */ 3370Sstevel@tonic-gate static int 3380Sstevel@tonic-gate keysock_param_get(q, mp, cp, cr) 3390Sstevel@tonic-gate queue_t *q; 3400Sstevel@tonic-gate mblk_t *mp; 3410Sstevel@tonic-gate caddr_t cp; 3420Sstevel@tonic-gate cred_t *cr; 3430Sstevel@tonic-gate { 3440Sstevel@tonic-gate keysockparam_t *keysockpa = (keysockparam_t *)cp; 3450Sstevel@tonic-gate uint_t value; 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate mutex_enter(&keysock_param_lock); 3480Sstevel@tonic-gate value = keysockpa->keysock_param_value; 3490Sstevel@tonic-gate mutex_exit(&keysock_param_lock); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate (void) mi_mpprintf(mp, "%u", value); 3520Sstevel@tonic-gate return (0); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* This routine sets an NDD variable in a keysockparam_t structure. */ 3560Sstevel@tonic-gate /* ARGSUSED */ 3570Sstevel@tonic-gate static int 3580Sstevel@tonic-gate keysock_param_set(q, mp, value, cp, cr) 3590Sstevel@tonic-gate queue_t *q; 3600Sstevel@tonic-gate mblk_t *mp; 3610Sstevel@tonic-gate char *value; 3620Sstevel@tonic-gate caddr_t cp; 3630Sstevel@tonic-gate cred_t *cr; 3640Sstevel@tonic-gate { 3650Sstevel@tonic-gate ulong_t new_value; 3660Sstevel@tonic-gate keysockparam_t *keysockpa = (keysockparam_t *)cp; 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate /* Convert the value from a string into a long integer. */ 3690Sstevel@tonic-gate if (ddi_strtoul(value, NULL, 10, &new_value) != 0) 3700Sstevel@tonic-gate return (EINVAL); 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate mutex_enter(&keysock_param_lock); 3730Sstevel@tonic-gate /* 3740Sstevel@tonic-gate * Fail the request if the new value does not lie within the 3750Sstevel@tonic-gate * required bounds. 3760Sstevel@tonic-gate */ 3770Sstevel@tonic-gate if (new_value < keysockpa->keysock_param_min || 3780Sstevel@tonic-gate new_value > keysockpa->keysock_param_max) { 3790Sstevel@tonic-gate mutex_exit(&keysock_param_lock); 3800Sstevel@tonic-gate return (EINVAL); 3810Sstevel@tonic-gate } 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate /* Set the new value */ 3840Sstevel@tonic-gate keysockpa->keysock_param_value = new_value; 3850Sstevel@tonic-gate mutex_exit(&keysock_param_lock); 3860Sstevel@tonic-gate 3870Sstevel@tonic-gate return (0); 3880Sstevel@tonic-gate } 3890Sstevel@tonic-gate 3900Sstevel@tonic-gate /* 3910Sstevel@tonic-gate * Initialize NDD variables, and other things, for keysock. 3920Sstevel@tonic-gate */ 3930Sstevel@tonic-gate boolean_t 3940Sstevel@tonic-gate keysock_ddi_init(void) 3950Sstevel@tonic-gate { 3960Sstevel@tonic-gate keysockparam_t *ksp = keysock_param_arr; 3970Sstevel@tonic-gate int count = A_CNT(keysock_param_arr); 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate if (!keysock_g_nd) { 4000Sstevel@tonic-gate for (; count-- > 0; ksp++) { 4010Sstevel@tonic-gate if (ksp->keysock_param_name != NULL && 4020Sstevel@tonic-gate ksp->keysock_param_name[0]) { 4030Sstevel@tonic-gate if (!nd_load(&keysock_g_nd, 4040Sstevel@tonic-gate ksp->keysock_param_name, 4050Sstevel@tonic-gate keysock_param_get, keysock_param_set, 4060Sstevel@tonic-gate (caddr_t)ksp)) { 4070Sstevel@tonic-gate nd_free(&keysock_g_nd); 4080Sstevel@tonic-gate return (B_FALSE); 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate } 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate keysock_max_optsize = optcom_max_optsize( 4150Sstevel@tonic-gate keysock_opt_obj.odb_opt_des_arr, keysock_opt_obj.odb_opt_arr_cnt); 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate keysock_vmem = vmem_create("keysock", (void *)1, MAXMIN, 1, 4180Sstevel@tonic-gate NULL, NULL, NULL, 1, VM_SLEEP | VMC_IDENTIFIER); 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate mutex_init(&keysock_list_lock, NULL, MUTEX_DEFAULT, NULL); 4210Sstevel@tonic-gate mutex_init(&keysock_consumers_lock, NULL, MUTEX_DEFAULT, NULL); 4220Sstevel@tonic-gate mutex_init(&keysock_param_lock, NULL, MUTEX_DEFAULT, NULL); 4230Sstevel@tonic-gate 4240Sstevel@tonic-gate return (B_TRUE); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * Free NDD variable space, and other destructors, for keysock. 4290Sstevel@tonic-gate */ 4300Sstevel@tonic-gate void 4310Sstevel@tonic-gate keysock_ddi_destroy(void) 4320Sstevel@tonic-gate { 4330Sstevel@tonic-gate /* XXX Free instances? */ 4340Sstevel@tonic-gate ks0dbg(("keysock_ddi_destroy being called.\n")); 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate vmem_destroy(keysock_vmem); 4370Sstevel@tonic-gate mutex_destroy(&keysock_list_lock); 4380Sstevel@tonic-gate mutex_destroy(&keysock_consumers_lock); 4390Sstevel@tonic-gate mutex_destroy(&keysock_param_lock); 4400Sstevel@tonic-gate nd_free(&keysock_g_nd); 4410Sstevel@tonic-gate } 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate /* 4440Sstevel@tonic-gate * Close routine for keysock. 4450Sstevel@tonic-gate */ 4460Sstevel@tonic-gate static int 4470Sstevel@tonic-gate keysock_close(queue_t *q) 4480Sstevel@tonic-gate { 4490Sstevel@tonic-gate keysock_t *ks; 4500Sstevel@tonic-gate keysock_consumer_t *kc; 4510Sstevel@tonic-gate void *ptr = q->q_ptr; 4520Sstevel@tonic-gate int size; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate qprocsoff(q); 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate /* Safe assumption. */ 4570Sstevel@tonic-gate ASSERT(ptr != NULL); 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate if (WR(q)->q_next) { 4600Sstevel@tonic-gate kc = (keysock_consumer_t *)ptr; 4610Sstevel@tonic-gate ks0dbg(("Module close, removing a consumer (%d).\n", 4620Sstevel@tonic-gate kc->kc_sa_type)); 4630Sstevel@tonic-gate /* 4640Sstevel@tonic-gate * Because of PERMOD open/close exclusive perimeter, I 4650Sstevel@tonic-gate * can inspect KC_FLUSHING w/o locking down kc->kc_lock. 4660Sstevel@tonic-gate */ 4670Sstevel@tonic-gate if (kc->kc_flags & KC_FLUSHING) { 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * If this decrement was the last one, send 4700Sstevel@tonic-gate * down the next pending one, if any. 4710Sstevel@tonic-gate * 4720Sstevel@tonic-gate * With a PERMOD perimeter, the mutexes ops aren't 4730Sstevel@tonic-gate * really necessary, but if we ever loosen up, we will 4740Sstevel@tonic-gate * have this bit covered already. 4750Sstevel@tonic-gate */ 4760Sstevel@tonic-gate keysock_flushdump--; 4770Sstevel@tonic-gate if (keysock_flushdump == 0) { 4780Sstevel@tonic-gate /* 4790Sstevel@tonic-gate * The flush/dump terminated by having a 4800Sstevel@tonic-gate * consumer go away. I need to send up to the 4810Sstevel@tonic-gate * appropriate keysock all of the relevant 4820Sstevel@tonic-gate * information. Unfortunately, I don't 4830Sstevel@tonic-gate * have that handy. 4840Sstevel@tonic-gate */ 4850Sstevel@tonic-gate ks0dbg(("Consumer went away while flushing or" 4860Sstevel@tonic-gate " dumping.\n")); 4870Sstevel@tonic-gate } 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate size = sizeof (keysock_consumer_t); 4900Sstevel@tonic-gate mutex_enter(&keysock_consumers_lock); 4910Sstevel@tonic-gate keysock_consumers[kc->kc_sa_type] = NULL; 4920Sstevel@tonic-gate mutex_exit(&keysock_consumers_lock); 4930Sstevel@tonic-gate mutex_destroy(&kc->kc_lock); 4940Sstevel@tonic-gate } else { 4950Sstevel@tonic-gate ks3dbg(("Driver close, PF_KEY socket is going away.\n")); 4960Sstevel@tonic-gate ks = (keysock_t *)ptr; 4970Sstevel@tonic-gate if ((ks->keysock_flags & KEYSOCK_EXTENDED) != 0) 4980Sstevel@tonic-gate atomic_add_32(&keysock_num_extended, -1); 4990Sstevel@tonic-gate size = sizeof (keysock_t); 5000Sstevel@tonic-gate mutex_enter(&keysock_list_lock); 5010Sstevel@tonic-gate *(ks->keysock_ptpn) = ks->keysock_next; 5020Sstevel@tonic-gate if (ks->keysock_next != NULL) 5030Sstevel@tonic-gate ks->keysock_next->keysock_ptpn = ks->keysock_ptpn; 5040Sstevel@tonic-gate mutex_exit(&keysock_list_lock); 5050Sstevel@tonic-gate mutex_destroy(&ks->keysock_lock); 506*2465Sdanmcd vmem_free(keysock_vmem, (void *)(uintptr_t)ks->keysock_serial, 507*2465Sdanmcd 1); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate 5100Sstevel@tonic-gate /* Now I'm free. */ 5110Sstevel@tonic-gate kmem_free(ptr, size); 5120Sstevel@tonic-gate return (0); 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate /* 5150Sstevel@tonic-gate * Open routine for keysock. 5160Sstevel@tonic-gate */ 5170Sstevel@tonic-gate /* ARGSUSED */ 5180Sstevel@tonic-gate static int 5190Sstevel@tonic-gate keysock_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 5200Sstevel@tonic-gate { 5210Sstevel@tonic-gate keysock_t *ks; 5220Sstevel@tonic-gate keysock_consumer_t *kc; 5230Sstevel@tonic-gate mblk_t *mp; 5240Sstevel@tonic-gate ipsec_info_t *ii; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate ks3dbg(("Entering keysock open.\n")); 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate if (secpolicy_net_config(credp, B_FALSE) != 0) { 5290Sstevel@tonic-gate /* Privilege debugging will log the error */ 5300Sstevel@tonic-gate return (EPERM); 5310Sstevel@tonic-gate } 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate if (q->q_ptr != NULL) 5340Sstevel@tonic-gate return (0); /* Re-open of an already open instance. */ 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate if (keysock_plumbed < 1) { 5370Sstevel@tonic-gate keysock_plumbed = 0; 5380Sstevel@tonic-gate /* 5390Sstevel@tonic-gate * Don't worry about ipsec_failure being true here. 5400Sstevel@tonic-gate * (See ip.c). An open of keysock should try and force 5410Sstevel@tonic-gate * the issue. Maybe it was a transient failure. 5420Sstevel@tonic-gate */ 5430Sstevel@tonic-gate ipsec_loader_loadnow(); 5440Sstevel@tonic-gate } 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate if (sflag & MODOPEN) { 5470Sstevel@tonic-gate /* Initialize keysock_consumer state here. */ 5480Sstevel@tonic-gate kc = kmem_zalloc(sizeof (keysock_consumer_t), KM_NOSLEEP); 5490Sstevel@tonic-gate if (kc == NULL) 5500Sstevel@tonic-gate return (ENOMEM); 5510Sstevel@tonic-gate mutex_init(&kc->kc_lock, NULL, MUTEX_DEFAULT, 0); 5520Sstevel@tonic-gate kc->kc_rq = q; 5530Sstevel@tonic-gate kc->kc_wq = WR(q); 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate q->q_ptr = kc; 5560Sstevel@tonic-gate WR(q)->q_ptr = kc; 5570Sstevel@tonic-gate 5580Sstevel@tonic-gate qprocson(q); 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate /* 5610Sstevel@tonic-gate * Send down initial message to whatever I was pushed on top 5620Sstevel@tonic-gate * of asking for its consumer type. The reply will set it. 5630Sstevel@tonic-gate */ 5640Sstevel@tonic-gate 5650Sstevel@tonic-gate /* Allocate it. */ 5660Sstevel@tonic-gate mp = allocb(sizeof (ipsec_info_t), BPRI_HI); 5670Sstevel@tonic-gate if (mp == NULL) { 5680Sstevel@tonic-gate ks1dbg(( 5690Sstevel@tonic-gate "keysock_open: Cannot allocate KEYSOCK_HELLO.\n")); 5700Sstevel@tonic-gate /* Do I need to set these to null? */ 5710Sstevel@tonic-gate q->q_ptr = NULL; 5720Sstevel@tonic-gate WR(q)->q_ptr = NULL; 5730Sstevel@tonic-gate mutex_destroy(&kc->kc_lock); 5740Sstevel@tonic-gate kmem_free(kc, sizeof (*kc)); 5750Sstevel@tonic-gate return (ENOMEM); 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate /* If I allocated okay, putnext to what I was pushed atop. */ 5790Sstevel@tonic-gate mp->b_wptr += sizeof (ipsec_info_t); 5800Sstevel@tonic-gate mp->b_datap->db_type = M_CTL; 5810Sstevel@tonic-gate ii = (ipsec_info_t *)mp->b_rptr; 5820Sstevel@tonic-gate ii->ipsec_info_type = KEYSOCK_HELLO; 5830Sstevel@tonic-gate /* Length only of type/len. */ 5840Sstevel@tonic-gate ii->ipsec_info_len = sizeof (ii->ipsec_allu); 5850Sstevel@tonic-gate ks2dbg(("Ready to putnext KEYSOCK_HELLO.\n")); 5860Sstevel@tonic-gate putnext(kc->kc_wq, mp); 5870Sstevel@tonic-gate } else { 5880Sstevel@tonic-gate minor_t ksminor; 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate /* Initialize keysock state. */ 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate ks2dbg(("Made it into PF_KEY socket open.\n")); 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate ksminor = (minor_t)(uintptr_t) 5950Sstevel@tonic-gate vmem_alloc(keysock_vmem, 1, VM_NOSLEEP); 5960Sstevel@tonic-gate if (ksminor == 0) 5970Sstevel@tonic-gate return (ENOMEM); 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate ks = kmem_zalloc(sizeof (keysock_t), KM_NOSLEEP); 6000Sstevel@tonic-gate if (ks == NULL) { 6010Sstevel@tonic-gate vmem_free(keysock_vmem, (void *)(uintptr_t)ksminor, 1); 6020Sstevel@tonic-gate return (ENOMEM); 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate mutex_init(&ks->keysock_lock, NULL, MUTEX_DEFAULT, 0); 6060Sstevel@tonic-gate ks->keysock_rq = q; 6070Sstevel@tonic-gate ks->keysock_wq = WR(q); 6080Sstevel@tonic-gate ks->keysock_state = TS_UNBND; 6090Sstevel@tonic-gate ks->keysock_serial = ksminor; 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate q->q_ptr = ks; 6120Sstevel@tonic-gate WR(q)->q_ptr = ks; 6130Sstevel@tonic-gate 6140Sstevel@tonic-gate /* 6150Sstevel@tonic-gate * The receive hiwat is only looked at on the stream head 6160Sstevel@tonic-gate * queue. Store in q_hiwat in order to return on SO_RCVBUF 6170Sstevel@tonic-gate * getsockopts. 6180Sstevel@tonic-gate */ 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate q->q_hiwat = keysock_recv_hiwat; 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate /* 6230Sstevel@tonic-gate * The transmit hiwat/lowat is only looked at on IP's queue. 6240Sstevel@tonic-gate * Store in q_hiwat/q_lowat in order to return on 6250Sstevel@tonic-gate * SO_SNDBUF/SO_SNDLOWAT getsockopts. 6260Sstevel@tonic-gate */ 6270Sstevel@tonic-gate 6280Sstevel@tonic-gate WR(q)->q_hiwat = keysock_xmit_hiwat; 6290Sstevel@tonic-gate WR(q)->q_lowat = keysock_xmit_lowat; 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate *devp = makedevice(getmajor(*devp), ksminor); 6320Sstevel@tonic-gate 6330Sstevel@tonic-gate /* 6340Sstevel@tonic-gate * Thread keysock into the global keysock list. 6350Sstevel@tonic-gate */ 6360Sstevel@tonic-gate mutex_enter(&keysock_list_lock); 6370Sstevel@tonic-gate ks->keysock_next = keysock_list; 6380Sstevel@tonic-gate ks->keysock_ptpn = &keysock_list; 6390Sstevel@tonic-gate if (keysock_list != NULL) 6400Sstevel@tonic-gate keysock_list->keysock_ptpn = &ks->keysock_next; 6410Sstevel@tonic-gate keysock_list = ks; 6420Sstevel@tonic-gate mutex_exit(&keysock_list_lock); 6430Sstevel@tonic-gate 6440Sstevel@tonic-gate qprocson(q); 6450Sstevel@tonic-gate (void) mi_set_sth_hiwat(q, keysock_recv_hiwat); 6460Sstevel@tonic-gate /* 6470Sstevel@tonic-gate * Wait outside the keysock module perimeter for IPsec 6480Sstevel@tonic-gate * plumbing to be completed. If it fails, keysock_close() 6490Sstevel@tonic-gate * undoes everything we just did. 6500Sstevel@tonic-gate */ 6510Sstevel@tonic-gate if (!ipsec_loader_wait(q)) { 6520Sstevel@tonic-gate (void) keysock_close(q); 6530Sstevel@tonic-gate return (EPFNOSUPPORT); 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate return (0); 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_wput(). */ 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate /* 6630Sstevel@tonic-gate * Copy relevant state bits. 6640Sstevel@tonic-gate */ 6650Sstevel@tonic-gate static void 6660Sstevel@tonic-gate keysock_copy_info(struct T_info_ack *tap, keysock_t *ks) 6670Sstevel@tonic-gate { 6680Sstevel@tonic-gate *tap = keysock_g_t_info_ack; 6690Sstevel@tonic-gate tap->CURRENT_state = ks->keysock_state; 6700Sstevel@tonic-gate tap->OPT_size = keysock_max_optsize; 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate /* 6740Sstevel@tonic-gate * This routine responds to T_CAPABILITY_REQ messages. It is called by 6750Sstevel@tonic-gate * keysock_wput. Much of the T_CAPABILITY_ACK information is copied from 6760Sstevel@tonic-gate * keysock_g_t_info_ack. The current state of the stream is copied from 6770Sstevel@tonic-gate * keysock_state. 6780Sstevel@tonic-gate */ 6790Sstevel@tonic-gate static void 6800Sstevel@tonic-gate keysock_capability_req(queue_t *q, mblk_t *mp) 6810Sstevel@tonic-gate { 6820Sstevel@tonic-gate keysock_t *ks = (keysock_t *)q->q_ptr; 6830Sstevel@tonic-gate t_uscalar_t cap_bits1; 6840Sstevel@tonic-gate struct T_capability_ack *tcap; 6850Sstevel@tonic-gate 6860Sstevel@tonic-gate cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 6890Sstevel@tonic-gate mp->b_datap->db_type, T_CAPABILITY_ACK); 6900Sstevel@tonic-gate if (mp == NULL) 6910Sstevel@tonic-gate return; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate tcap = (struct T_capability_ack *)mp->b_rptr; 6940Sstevel@tonic-gate tcap->CAP_bits1 = 0; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate if (cap_bits1 & TC1_INFO) { 6970Sstevel@tonic-gate keysock_copy_info(&tcap->INFO_ack, ks); 6980Sstevel@tonic-gate tcap->CAP_bits1 |= TC1_INFO; 6990Sstevel@tonic-gate } 7000Sstevel@tonic-gate 7010Sstevel@tonic-gate qreply(q, mp); 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate /* 7050Sstevel@tonic-gate * This routine responds to T_INFO_REQ messages. It is called by 7060Sstevel@tonic-gate * keysock_wput_other. 7070Sstevel@tonic-gate * Most of the T_INFO_ACK information is copied from keysock_g_t_info_ack. 7080Sstevel@tonic-gate * The current state of the stream is copied from keysock_state. 7090Sstevel@tonic-gate */ 7100Sstevel@tonic-gate static void 7110Sstevel@tonic-gate keysock_info_req(q, mp) 7120Sstevel@tonic-gate queue_t *q; 7130Sstevel@tonic-gate mblk_t *mp; 7140Sstevel@tonic-gate { 7150Sstevel@tonic-gate mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7160Sstevel@tonic-gate T_INFO_ACK); 7170Sstevel@tonic-gate if (mp == NULL) 7180Sstevel@tonic-gate return; 7190Sstevel@tonic-gate keysock_copy_info((struct T_info_ack *)mp->b_rptr, 7200Sstevel@tonic-gate (keysock_t *)q->q_ptr); 7210Sstevel@tonic-gate qreply(q, mp); 7220Sstevel@tonic-gate } 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate /* 7250Sstevel@tonic-gate * keysock_err_ack. This routine creates a 7260Sstevel@tonic-gate * T_ERROR_ACK message and passes it 7270Sstevel@tonic-gate * upstream. 7280Sstevel@tonic-gate */ 7290Sstevel@tonic-gate static void 7300Sstevel@tonic-gate keysock_err_ack(q, mp, t_error, sys_error) 7310Sstevel@tonic-gate queue_t *q; 7320Sstevel@tonic-gate mblk_t *mp; 7330Sstevel@tonic-gate int t_error; 7340Sstevel@tonic-gate int sys_error; 7350Sstevel@tonic-gate { 7360Sstevel@tonic-gate if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7370Sstevel@tonic-gate qreply(q, mp); 7380Sstevel@tonic-gate } 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate /* 7410Sstevel@tonic-gate * This routine retrieves the current status of socket options. 7420Sstevel@tonic-gate * It returns the size of the option retrieved. 7430Sstevel@tonic-gate */ 7440Sstevel@tonic-gate /* ARGSUSED */ 7450Sstevel@tonic-gate int 7460Sstevel@tonic-gate keysock_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 7470Sstevel@tonic-gate { 7480Sstevel@tonic-gate int *i1 = (int *)ptr; 7490Sstevel@tonic-gate keysock_t *ks = (keysock_t *)q->q_ptr; 7500Sstevel@tonic-gate 7510Sstevel@tonic-gate switch (level) { 7520Sstevel@tonic-gate case SOL_SOCKET: 7530Sstevel@tonic-gate mutex_enter(&ks->keysock_lock); 7540Sstevel@tonic-gate switch (name) { 7550Sstevel@tonic-gate case SO_TYPE: 7560Sstevel@tonic-gate *i1 = SOCK_RAW; 7570Sstevel@tonic-gate break; 7580Sstevel@tonic-gate case SO_USELOOPBACK: 7590Sstevel@tonic-gate *i1 = (int)(!((ks->keysock_flags & KEYSOCK_NOLOOP) == 7600Sstevel@tonic-gate KEYSOCK_NOLOOP)); 7610Sstevel@tonic-gate break; 7620Sstevel@tonic-gate /* 7630Sstevel@tonic-gate * The following two items can be manipulated, 7640Sstevel@tonic-gate * but changing them should do nothing. 7650Sstevel@tonic-gate */ 7660Sstevel@tonic-gate case SO_SNDBUF: 7670Sstevel@tonic-gate *i1 = (int)q->q_hiwat; 7680Sstevel@tonic-gate break; 7690Sstevel@tonic-gate case SO_RCVBUF: 7700Sstevel@tonic-gate *i1 = (int)(RD(q)->q_hiwat); 7710Sstevel@tonic-gate break; 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate mutex_exit(&ks->keysock_lock); 7740Sstevel@tonic-gate break; 7750Sstevel@tonic-gate default: 7760Sstevel@tonic-gate return (0); 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate return (sizeof (int)); 7790Sstevel@tonic-gate } 7800Sstevel@tonic-gate 7810Sstevel@tonic-gate /* 7820Sstevel@tonic-gate * This routine sets socket options. 7830Sstevel@tonic-gate */ 7840Sstevel@tonic-gate /* ARGSUSED */ 7850Sstevel@tonic-gate int 7860Sstevel@tonic-gate keysock_opt_set(queue_t *q, uint_t mgmt_flags, int level, 7870Sstevel@tonic-gate int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 7880Sstevel@tonic-gate uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 7890Sstevel@tonic-gate { 7900Sstevel@tonic-gate int *i1 = (int *)invalp; 7910Sstevel@tonic-gate keysock_t *ks = (keysock_t *)q->q_ptr; 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate switch (level) { 7940Sstevel@tonic-gate case SOL_SOCKET: 7950Sstevel@tonic-gate mutex_enter(&ks->keysock_lock); 7960Sstevel@tonic-gate switch (name) { 7970Sstevel@tonic-gate case SO_USELOOPBACK: 7980Sstevel@tonic-gate if (!(*i1)) 7990Sstevel@tonic-gate ks->keysock_flags |= KEYSOCK_NOLOOP; 8000Sstevel@tonic-gate else ks->keysock_flags &= ~KEYSOCK_NOLOOP; 8010Sstevel@tonic-gate break; 8020Sstevel@tonic-gate case SO_SNDBUF: 8030Sstevel@tonic-gate if (*i1 > keysock_max_buf) 8040Sstevel@tonic-gate return (ENOBUFS); 8050Sstevel@tonic-gate q->q_hiwat = *i1; 8060Sstevel@tonic-gate break; 8070Sstevel@tonic-gate case SO_RCVBUF: 8080Sstevel@tonic-gate if (*i1 > keysock_max_buf) 8090Sstevel@tonic-gate return (ENOBUFS); 8100Sstevel@tonic-gate RD(q)->q_hiwat = *i1; 8110Sstevel@tonic-gate (void) mi_set_sth_hiwat(RD(q), *i1); 8120Sstevel@tonic-gate break; 8130Sstevel@tonic-gate } 8140Sstevel@tonic-gate mutex_exit(&ks->keysock_lock); 8150Sstevel@tonic-gate break; 8160Sstevel@tonic-gate } 8170Sstevel@tonic-gate return (0); 8180Sstevel@tonic-gate } 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate /* 8210Sstevel@tonic-gate * Handle STREAMS messages. 8220Sstevel@tonic-gate */ 8230Sstevel@tonic-gate static void 8240Sstevel@tonic-gate keysock_wput_other(queue_t *q, mblk_t *mp) 8250Sstevel@tonic-gate { 8260Sstevel@tonic-gate struct iocblk *iocp; 8270Sstevel@tonic-gate int error; 8280Sstevel@tonic-gate 8290Sstevel@tonic-gate switch (mp->b_datap->db_type) { 8300Sstevel@tonic-gate case M_PROTO: 8310Sstevel@tonic-gate case M_PCPROTO: 8320Sstevel@tonic-gate if ((mp->b_wptr - mp->b_rptr) < sizeof (long)) { 8330Sstevel@tonic-gate ks3dbg(( 8340Sstevel@tonic-gate "keysock_wput_other: Not big enough M_PROTO\n")); 8350Sstevel@tonic-gate freemsg(mp); 8360Sstevel@tonic-gate return; 8370Sstevel@tonic-gate } 8380Sstevel@tonic-gate switch (((union T_primitives *)mp->b_rptr)->type) { 8390Sstevel@tonic-gate case T_CAPABILITY_REQ: 8400Sstevel@tonic-gate keysock_capability_req(q, mp); 8410Sstevel@tonic-gate return; 8420Sstevel@tonic-gate case T_INFO_REQ: 8430Sstevel@tonic-gate keysock_info_req(q, mp); 8440Sstevel@tonic-gate return; 8450Sstevel@tonic-gate case T_SVR4_OPTMGMT_REQ: 8460Sstevel@tonic-gate (void) svr4_optcom_req(q, mp, DB_CREDDEF(mp, kcred), 8470Sstevel@tonic-gate &keysock_opt_obj); 8480Sstevel@tonic-gate return; 8490Sstevel@tonic-gate case T_OPTMGMT_REQ: 8500Sstevel@tonic-gate (void) tpi_optcom_req(q, mp, DB_CREDDEF(mp, kcred), 8510Sstevel@tonic-gate &keysock_opt_obj); 8520Sstevel@tonic-gate return; 8530Sstevel@tonic-gate case T_DATA_REQ: 8540Sstevel@tonic-gate case T_EXDATA_REQ: 8550Sstevel@tonic-gate case T_ORDREL_REQ: 8560Sstevel@tonic-gate /* Illegal for keysock. */ 8570Sstevel@tonic-gate freemsg(mp); 8580Sstevel@tonic-gate (void) putnextctl1(RD(q), M_ERROR, EPROTO); 8590Sstevel@tonic-gate return; 8600Sstevel@tonic-gate default: 8610Sstevel@tonic-gate /* Not supported by keysock. */ 8620Sstevel@tonic-gate keysock_err_ack(q, mp, TNOTSUPPORT, 0); 8630Sstevel@tonic-gate return; 8640Sstevel@tonic-gate } 8650Sstevel@tonic-gate case M_IOCTL: 8660Sstevel@tonic-gate iocp = (struct iocblk *)mp->b_rptr; 8670Sstevel@tonic-gate error = EINVAL; 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate switch (iocp->ioc_cmd) { 8700Sstevel@tonic-gate case ND_SET: 8710Sstevel@tonic-gate case ND_GET: 8720Sstevel@tonic-gate if (nd_getset(q, keysock_g_nd, mp)) { 8730Sstevel@tonic-gate qreply(q, mp); 8740Sstevel@tonic-gate return; 8750Sstevel@tonic-gate } else 8760Sstevel@tonic-gate error = ENOENT; 8770Sstevel@tonic-gate /* FALLTHRU */ 8780Sstevel@tonic-gate default: 8790Sstevel@tonic-gate miocnak(q, mp, 0, error); 8800Sstevel@tonic-gate return; 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate case M_FLUSH: 8830Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) { 8840Sstevel@tonic-gate flushq(q, FLUSHALL); 8850Sstevel@tonic-gate *mp->b_rptr &= ~FLUSHW; 8860Sstevel@tonic-gate } 8870Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) { 8880Sstevel@tonic-gate qreply(q, mp); 8890Sstevel@tonic-gate return; 8900Sstevel@tonic-gate } 8910Sstevel@tonic-gate /* Else FALLTHRU */ 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate /* If fell through, just black-hole the message. */ 8950Sstevel@tonic-gate freemsg(mp); 8960Sstevel@tonic-gate } 8970Sstevel@tonic-gate 8980Sstevel@tonic-gate /* 8990Sstevel@tonic-gate * Transmit a PF_KEY error message to the instance either pointed to 9000Sstevel@tonic-gate * by ks, the instance with serial number serial, or more, depending. 9010Sstevel@tonic-gate * 9020Sstevel@tonic-gate * The faulty message (or a reasonable facsimile thereof) is in mp. 9030Sstevel@tonic-gate * This function will free mp or recycle it for delivery, thereby causing 9040Sstevel@tonic-gate * the stream head to free it. 9050Sstevel@tonic-gate */ 9060Sstevel@tonic-gate static void 9070Sstevel@tonic-gate keysock_error(keysock_t *ks, mblk_t *mp, int error, int diagnostic) 9080Sstevel@tonic-gate { 9090Sstevel@tonic-gate sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr; 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate ASSERT(mp->b_datap->db_type == M_DATA); 9120Sstevel@tonic-gate 9130Sstevel@tonic-gate if (samsg->sadb_msg_type < SADB_GETSPI || 9140Sstevel@tonic-gate samsg->sadb_msg_type > SADB_MAX) 9150Sstevel@tonic-gate samsg->sadb_msg_type = SADB_RESERVED; 9160Sstevel@tonic-gate 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * Strip out extension headers. 9190Sstevel@tonic-gate */ 9200Sstevel@tonic-gate ASSERT(mp->b_rptr + sizeof (*samsg) <= mp->b_datap->db_lim); 9210Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (*samsg); 9220Sstevel@tonic-gate samsg->sadb_msg_len = SADB_8TO64(sizeof (sadb_msg_t)); 9230Sstevel@tonic-gate samsg->sadb_msg_errno = (uint8_t)error; 9240Sstevel@tonic-gate samsg->sadb_x_msg_diagnostic = (uint16_t)diagnostic; 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate /* 9300Sstevel@tonic-gate * Pass down a message to a consumer. Wrap it in KEYSOCK_IN, and copy 9310Sstevel@tonic-gate * in the extv if passed in. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate static void 9340Sstevel@tonic-gate keysock_passdown(keysock_t *ks, mblk_t *mp, uint8_t satype, sadb_ext_t *extv[], 9350Sstevel@tonic-gate boolean_t flushmsg) 9360Sstevel@tonic-gate { 9370Sstevel@tonic-gate keysock_consumer_t *kc; 9380Sstevel@tonic-gate mblk_t *wrapper; 9390Sstevel@tonic-gate keysock_in_t *ksi; 9400Sstevel@tonic-gate int i; 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate wrapper = allocb(sizeof (ipsec_info_t), BPRI_HI); 9430Sstevel@tonic-gate if (wrapper == NULL) { 9440Sstevel@tonic-gate ks3dbg(("keysock_passdown: allocb failed.\n")); 9450Sstevel@tonic-gate if (extv[SADB_EXT_KEY_ENCRYPT] != NULL) 9460Sstevel@tonic-gate bzero(extv[SADB_EXT_KEY_ENCRYPT], 9470Sstevel@tonic-gate SADB_64TO8( 9480Sstevel@tonic-gate extv[SADB_EXT_KEY_ENCRYPT]->sadb_ext_len)); 9490Sstevel@tonic-gate if (extv[SADB_EXT_KEY_AUTH] != NULL) 9500Sstevel@tonic-gate bzero(extv[SADB_EXT_KEY_AUTH], 9510Sstevel@tonic-gate SADB_64TO8( 9520Sstevel@tonic-gate extv[SADB_EXT_KEY_AUTH]->sadb_ext_len)); 9530Sstevel@tonic-gate if (flushmsg) { 9540Sstevel@tonic-gate ks0dbg(( 9550Sstevel@tonic-gate "keysock: Downwards flush/dump message failed!\n")); 9560Sstevel@tonic-gate /* If this is true, I hold the perimeter. */ 9570Sstevel@tonic-gate keysock_flushdump--; 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate freemsg(mp); 9600Sstevel@tonic-gate return; 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate wrapper->b_datap->db_type = M_CTL; 9640Sstevel@tonic-gate ksi = (keysock_in_t *)wrapper->b_rptr; 9650Sstevel@tonic-gate ksi->ks_in_type = KEYSOCK_IN; 9660Sstevel@tonic-gate ksi->ks_in_len = sizeof (keysock_in_t); 9670Sstevel@tonic-gate if (extv[SADB_EXT_ADDRESS_SRC] != NULL) 9680Sstevel@tonic-gate ksi->ks_in_srctype = KS_IN_ADDR_UNKNOWN; 9690Sstevel@tonic-gate else ksi->ks_in_srctype = KS_IN_ADDR_NOTTHERE; 9700Sstevel@tonic-gate if (extv[SADB_EXT_ADDRESS_DST] != NULL) 9710Sstevel@tonic-gate ksi->ks_in_dsttype = KS_IN_ADDR_UNKNOWN; 9720Sstevel@tonic-gate else ksi->ks_in_dsttype = KS_IN_ADDR_NOTTHERE; 9730Sstevel@tonic-gate if (extv[SADB_EXT_ADDRESS_PROXY] != NULL) 9740Sstevel@tonic-gate ksi->ks_in_proxytype = KS_IN_ADDR_UNKNOWN; 9750Sstevel@tonic-gate else ksi->ks_in_proxytype = KS_IN_ADDR_NOTTHERE; 9760Sstevel@tonic-gate for (i = 0; i <= SADB_EXT_MAX; i++) 9770Sstevel@tonic-gate ksi->ks_in_extv[i] = extv[i]; 9780Sstevel@tonic-gate ksi->ks_in_serial = ks->keysock_serial; 9790Sstevel@tonic-gate wrapper->b_wptr += sizeof (ipsec_info_t); 9800Sstevel@tonic-gate wrapper->b_cont = mp; 9810Sstevel@tonic-gate 9820Sstevel@tonic-gate /* 9830Sstevel@tonic-gate * Find the appropriate consumer where the message is passed down. 9840Sstevel@tonic-gate */ 9850Sstevel@tonic-gate kc = keysock_consumers[satype]; 9860Sstevel@tonic-gate if (kc == NULL) { 9870Sstevel@tonic-gate freeb(wrapper); 9880Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE); 9890Sstevel@tonic-gate if (flushmsg) { 9900Sstevel@tonic-gate ks0dbg(( 9910Sstevel@tonic-gate "keysock: Downwards flush/dump message failed!\n")); 9920Sstevel@tonic-gate /* If this is true, I hold the perimeter. */ 9930Sstevel@tonic-gate keysock_flushdump--; 9940Sstevel@tonic-gate } 9950Sstevel@tonic-gate return; 9960Sstevel@tonic-gate } 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate /* 9990Sstevel@tonic-gate * NOTE: There used to be code in here to spin while a flush or 10000Sstevel@tonic-gate * dump finished. Keysock now assumes that consumers have enough 10010Sstevel@tonic-gate * MT-savviness to deal with that. 10020Sstevel@tonic-gate */ 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate /* 10050Sstevel@tonic-gate * Current consumers (AH and ESP) are guaranteed to return a 10060Sstevel@tonic-gate * FLUSH or DUMP message back, so when we reach here, we don't 10070Sstevel@tonic-gate * have to worry about keysock_flushdumps. 10080Sstevel@tonic-gate */ 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate putnext(kc->kc_wq, wrapper); 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate /* 10140Sstevel@tonic-gate * High-level reality checking of extensions. 10150Sstevel@tonic-gate */ 10160Sstevel@tonic-gate static boolean_t 10170Sstevel@tonic-gate ext_check(sadb_ext_t *ext) 10180Sstevel@tonic-gate { 10190Sstevel@tonic-gate int i; 10200Sstevel@tonic-gate uint64_t *lp; 10210Sstevel@tonic-gate sadb_ident_t *id; 10220Sstevel@tonic-gate char *idstr; 10230Sstevel@tonic-gate 10240Sstevel@tonic-gate switch (ext->sadb_ext_type) { 10250Sstevel@tonic-gate case SADB_EXT_ADDRESS_SRC: 10260Sstevel@tonic-gate case SADB_EXT_ADDRESS_DST: 10270Sstevel@tonic-gate case SADB_EXT_ADDRESS_PROXY: 10280Sstevel@tonic-gate /* Check for at least enough addtl length for a sockaddr. */ 10290Sstevel@tonic-gate if (ext->sadb_ext_len <= SADB_8TO64(sizeof (sadb_address_t))) 10300Sstevel@tonic-gate return (B_FALSE); 10310Sstevel@tonic-gate break; 10320Sstevel@tonic-gate case SADB_EXT_LIFETIME_HARD: 10330Sstevel@tonic-gate case SADB_EXT_LIFETIME_SOFT: 10340Sstevel@tonic-gate case SADB_EXT_LIFETIME_CURRENT: 10350Sstevel@tonic-gate if (ext->sadb_ext_len != SADB_8TO64(sizeof (sadb_lifetime_t))) 10360Sstevel@tonic-gate return (B_FALSE); 10370Sstevel@tonic-gate break; 10380Sstevel@tonic-gate case SADB_EXT_SPIRANGE: 10390Sstevel@tonic-gate /* See if the SPI range is legit. */ 10400Sstevel@tonic-gate if (htonl(((sadb_spirange_t *)ext)->sadb_spirange_min) > 10410Sstevel@tonic-gate htonl(((sadb_spirange_t *)ext)->sadb_spirange_max)) 10420Sstevel@tonic-gate return (B_FALSE); 10430Sstevel@tonic-gate break; 10440Sstevel@tonic-gate case SADB_EXT_KEY_AUTH: 10450Sstevel@tonic-gate case SADB_EXT_KEY_ENCRYPT: 10460Sstevel@tonic-gate /* Key length check. */ 10470Sstevel@tonic-gate if (((sadb_key_t *)ext)->sadb_key_bits == 0) 10480Sstevel@tonic-gate return (B_FALSE); 10490Sstevel@tonic-gate /* 10500Sstevel@tonic-gate * Check to see if the key length (in bits) is less than the 10510Sstevel@tonic-gate * extension length (in 8-bits words). 10520Sstevel@tonic-gate */ 10530Sstevel@tonic-gate if ((roundup(SADB_1TO8(((sadb_key_t *)ext)->sadb_key_bits), 8) + 10540Sstevel@tonic-gate sizeof (sadb_key_t)) != SADB_64TO8(ext->sadb_ext_len)) { 10550Sstevel@tonic-gate ks1dbg(( 10560Sstevel@tonic-gate "ext_check: Key bits/length inconsistent.\n")); 10570Sstevel@tonic-gate ks1dbg(("%d bits, len is %d bytes.\n", 10580Sstevel@tonic-gate ((sadb_key_t *)ext)->sadb_key_bits, 10590Sstevel@tonic-gate SADB_64TO8(ext->sadb_ext_len))); 10600Sstevel@tonic-gate return (B_FALSE); 10610Sstevel@tonic-gate } 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate /* All-zeroes key check. */ 10640Sstevel@tonic-gate lp = (uint64_t *)(((char *)ext) + sizeof (sadb_key_t)); 10650Sstevel@tonic-gate for (i = 0; 10660Sstevel@tonic-gate i < (ext->sadb_ext_len - SADB_8TO64(sizeof (sadb_key_t))); 10670Sstevel@tonic-gate i++) 10680Sstevel@tonic-gate if (lp[i] != 0) 10690Sstevel@tonic-gate break; /* Out of for loop. */ 10700Sstevel@tonic-gate /* If finished the loop naturally, it's an all zero key. */ 10710Sstevel@tonic-gate if (lp[i] == 0) 10720Sstevel@tonic-gate return (B_FALSE); 10730Sstevel@tonic-gate break; 10740Sstevel@tonic-gate case SADB_EXT_IDENTITY_SRC: 10750Sstevel@tonic-gate case SADB_EXT_IDENTITY_DST: 10760Sstevel@tonic-gate /* 10770Sstevel@tonic-gate * Make sure the strings in these identities are 10780Sstevel@tonic-gate * null-terminated. RFC 2367 underspecified how to handle 10790Sstevel@tonic-gate * such a case. I "proactively" null-terminate the string 10800Sstevel@tonic-gate * at the last byte if it's not terminated sooner. 10810Sstevel@tonic-gate */ 10820Sstevel@tonic-gate id = (sadb_ident_t *)ext; 10830Sstevel@tonic-gate i = SADB_64TO8(id->sadb_ident_len); 10840Sstevel@tonic-gate i -= sizeof (sadb_ident_t); 10850Sstevel@tonic-gate idstr = (char *)(id + 1); 10860Sstevel@tonic-gate while (*idstr != '\0' && i > 0) { 10870Sstevel@tonic-gate i--; 10880Sstevel@tonic-gate idstr++; 10890Sstevel@tonic-gate } 10900Sstevel@tonic-gate if (i == 0) { 10910Sstevel@tonic-gate /* 10920Sstevel@tonic-gate * I.e., if the bozo user didn't NULL-terminate the 10930Sstevel@tonic-gate * string... 10940Sstevel@tonic-gate */ 10950Sstevel@tonic-gate idstr--; 10960Sstevel@tonic-gate *idstr = '\0'; 10970Sstevel@tonic-gate } 10980Sstevel@tonic-gate break; 10990Sstevel@tonic-gate } 11000Sstevel@tonic-gate return (B_TRUE); /* For now... */ 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate 11030Sstevel@tonic-gate /* Return values for keysock_get_ext(). */ 11040Sstevel@tonic-gate #define KGE_OK 0 11050Sstevel@tonic-gate #define KGE_DUP 1 11060Sstevel@tonic-gate #define KGE_UNK 2 11070Sstevel@tonic-gate #define KGE_LEN 3 11080Sstevel@tonic-gate #define KGE_CHK 4 11090Sstevel@tonic-gate 11100Sstevel@tonic-gate /* 11110Sstevel@tonic-gate * Parse basic extension headers and return in the passed-in pointer vector. 11120Sstevel@tonic-gate * Return values include: 11130Sstevel@tonic-gate * 11140Sstevel@tonic-gate * KGE_OK Everything's nice and parsed out. 11150Sstevel@tonic-gate * If there are no extensions, place NULL in extv[0]. 11160Sstevel@tonic-gate * KGE_DUP There is a duplicate extension. 11170Sstevel@tonic-gate * First instance in appropriate bin. First duplicate in 11180Sstevel@tonic-gate * extv[0]. 11190Sstevel@tonic-gate * KGE_UNK Unknown extension type encountered. extv[0] contains 11200Sstevel@tonic-gate * unknown header. 11210Sstevel@tonic-gate * KGE_LEN Extension length error. 11220Sstevel@tonic-gate * KGE_CHK High-level reality check failed on specific extension. 11230Sstevel@tonic-gate * 11240Sstevel@tonic-gate * My apologies for some of the pointer arithmetic in here. I'm thinking 11250Sstevel@tonic-gate * like an assembly programmer, yet trying to make the compiler happy. 11260Sstevel@tonic-gate */ 11270Sstevel@tonic-gate static int 11280Sstevel@tonic-gate keysock_get_ext(sadb_ext_t *extv[], sadb_msg_t *basehdr, uint_t msgsize) 11290Sstevel@tonic-gate { 11300Sstevel@tonic-gate bzero(extv, sizeof (sadb_ext_t *) * (SADB_EXT_MAX + 1)); 11310Sstevel@tonic-gate 11320Sstevel@tonic-gate /* Use extv[0] as the "current working pointer". */ 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate extv[0] = (sadb_ext_t *)(basehdr + 1); 11350Sstevel@tonic-gate 11360Sstevel@tonic-gate while (extv[0] < (sadb_ext_t *)(((uint8_t *)basehdr) + msgsize)) { 11370Sstevel@tonic-gate /* Check for unknown headers. */ 11380Sstevel@tonic-gate if (extv[0]->sadb_ext_type == 0 || 11390Sstevel@tonic-gate extv[0]->sadb_ext_type > SADB_EXT_MAX) 11400Sstevel@tonic-gate return (KGE_UNK); 11410Sstevel@tonic-gate 11420Sstevel@tonic-gate /* 11430Sstevel@tonic-gate * Check length. Use uint64_t because extlen is in units 11440Sstevel@tonic-gate * of 64-bit words. If length goes beyond the msgsize, 11450Sstevel@tonic-gate * return an error. (Zero length also qualifies here.) 11460Sstevel@tonic-gate */ 11470Sstevel@tonic-gate if (extv[0]->sadb_ext_len == 0 || 11480Sstevel@tonic-gate (void *)((uint64_t *)extv[0] + extv[0]->sadb_ext_len) > 11490Sstevel@tonic-gate (void *)((uint8_t *)basehdr + msgsize)) 11500Sstevel@tonic-gate return (KGE_LEN); 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate /* Check for redundant headers. */ 11530Sstevel@tonic-gate if (extv[extv[0]->sadb_ext_type] != NULL) 11540Sstevel@tonic-gate return (KGE_DUP); 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate /* 11570Sstevel@tonic-gate * Reality check the extension if possible at the keysock 11580Sstevel@tonic-gate * level. 11590Sstevel@tonic-gate */ 11600Sstevel@tonic-gate if (!ext_check(extv[0])) 11610Sstevel@tonic-gate return (KGE_CHK); 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate /* If I make it here, assign the appropriate bin. */ 11640Sstevel@tonic-gate extv[extv[0]->sadb_ext_type] = extv[0]; 11650Sstevel@tonic-gate 11660Sstevel@tonic-gate /* Advance pointer (See above for uint64_t ptr reasoning.) */ 11670Sstevel@tonic-gate extv[0] = (sadb_ext_t *) 11680Sstevel@tonic-gate ((uint64_t *)extv[0] + extv[0]->sadb_ext_len); 11690Sstevel@tonic-gate } 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate /* Everything's cool. */ 11720Sstevel@tonic-gate 11730Sstevel@tonic-gate /* 11740Sstevel@tonic-gate * If extv[0] == NULL, then there are no extension headers in this 11750Sstevel@tonic-gate * message. Ensure that this is the case. 11760Sstevel@tonic-gate */ 11770Sstevel@tonic-gate if (extv[0] == (sadb_ext_t *)(basehdr + 1)) 11780Sstevel@tonic-gate extv[0] = NULL; 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate return (KGE_OK); 11810Sstevel@tonic-gate } 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate /* 11840Sstevel@tonic-gate * qwriter() callback to handle flushes and dumps. This routine will hold 11850Sstevel@tonic-gate * the inner perimeter. 11860Sstevel@tonic-gate */ 11870Sstevel@tonic-gate void 11880Sstevel@tonic-gate keysock_do_flushdump(queue_t *q, mblk_t *mp) 11890Sstevel@tonic-gate { 11900Sstevel@tonic-gate int i, start, finish; 11910Sstevel@tonic-gate mblk_t *mp1 = NULL; 11920Sstevel@tonic-gate keysock_t *ks = (keysock_t *)q->q_ptr; 11930Sstevel@tonic-gate sadb_ext_t *extv[SADB_EXT_MAX + 1]; 11940Sstevel@tonic-gate sadb_msg_t *samsg = (sadb_msg_t *)mp->b_rptr; 11950Sstevel@tonic-gate 11960Sstevel@tonic-gate /* 11970Sstevel@tonic-gate * I am guaranteed this will work. I did the work in keysock_parse() 11980Sstevel@tonic-gate * already. 11990Sstevel@tonic-gate */ 12000Sstevel@tonic-gate (void) keysock_get_ext(extv, samsg, SADB_64TO8(samsg->sadb_msg_len)); 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate /* 12030Sstevel@tonic-gate * I hold the perimeter, therefore I don't need to use atomic ops. 12040Sstevel@tonic-gate */ 12050Sstevel@tonic-gate if (keysock_flushdump != 0) { 12060Sstevel@tonic-gate /* XXX Should I instead use EBUSY? */ 12070Sstevel@tonic-gate /* XXX Or is there a way to queue these up? */ 12080Sstevel@tonic-gate keysock_error(ks, mp, ENOMEM, SADB_X_DIAGNOSTIC_NONE); 12090Sstevel@tonic-gate return; 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate 12120Sstevel@tonic-gate if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) { 12130Sstevel@tonic-gate start = 0; 12140Sstevel@tonic-gate finish = KEYSOCK_MAX_CONSUMERS - 1; 12150Sstevel@tonic-gate } else { 12160Sstevel@tonic-gate start = samsg->sadb_msg_satype; 12170Sstevel@tonic-gate finish = samsg->sadb_msg_satype; 12180Sstevel@tonic-gate } 12190Sstevel@tonic-gate 12200Sstevel@tonic-gate /* 12210Sstevel@tonic-gate * Fill up keysock_flushdump with the number of outstanding dumps 12220Sstevel@tonic-gate * and/or flushes. 12230Sstevel@tonic-gate */ 12240Sstevel@tonic-gate 12250Sstevel@tonic-gate keysock_flushdump_errno = 0; 12260Sstevel@tonic-gate 12270Sstevel@tonic-gate /* 12280Sstevel@tonic-gate * Okay, I hold the perimeter. Eventually keysock_flushdump will 12290Sstevel@tonic-gate * contain the number of consumers with outstanding flush operations. 12300Sstevel@tonic-gate * 12310Sstevel@tonic-gate * SO, here's the plan: 12320Sstevel@tonic-gate * * For each relevant consumer (Might be one, might be all) 12330Sstevel@tonic-gate * * Twiddle on the FLUSHING flag. 12340Sstevel@tonic-gate * * Pass down the FLUSH/DUMP message. 12350Sstevel@tonic-gate * 12360Sstevel@tonic-gate * When I see upbound FLUSH/DUMP messages, I will decrement the 12370Sstevel@tonic-gate * keysock_flushdump. When I decrement it to 0, I will pass the 12380Sstevel@tonic-gate * FLUSH/DUMP message back up to the PF_KEY sockets. Because I will 12390Sstevel@tonic-gate * pass down the right SA type to the consumer (either its own, or 12400Sstevel@tonic-gate * that of UNSPEC), the right one will be reflected from each consumer, 12410Sstevel@tonic-gate * and accordingly back to the socket. 12420Sstevel@tonic-gate */ 12430Sstevel@tonic-gate 12440Sstevel@tonic-gate mutex_enter(&keysock_consumers_lock); 12450Sstevel@tonic-gate for (i = start; i <= finish; i++) { 12460Sstevel@tonic-gate if (keysock_consumers[i] != NULL) { 12470Sstevel@tonic-gate mp1 = copymsg(mp); 12480Sstevel@tonic-gate if (mp1 == NULL) { 12490Sstevel@tonic-gate ks0dbg(("SADB_FLUSH copymsg() failed.\n")); 12500Sstevel@tonic-gate /* 12510Sstevel@tonic-gate * Error? And what about outstanding 12520Sstevel@tonic-gate * flushes? Oh, yeah, they get sucked up and 12530Sstevel@tonic-gate * the counter is decremented. Consumers 12540Sstevel@tonic-gate * (see keysock_passdown()) are guaranteed 12550Sstevel@tonic-gate * to deliver back a flush request, even if 12560Sstevel@tonic-gate * it's an error. 12570Sstevel@tonic-gate */ 12580Sstevel@tonic-gate keysock_error(ks, mp, ENOMEM, 12590Sstevel@tonic-gate SADB_X_DIAGNOSTIC_NONE); 12600Sstevel@tonic-gate return; 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate /* 12630Sstevel@tonic-gate * Because my entry conditions are met above, the 12640Sstevel@tonic-gate * following assertion should hold true. 12650Sstevel@tonic-gate */ 12660Sstevel@tonic-gate mutex_enter(&(keysock_consumers[i]->kc_lock)); 12670Sstevel@tonic-gate ASSERT((keysock_consumers[i]->kc_flags & KC_FLUSHING) 12680Sstevel@tonic-gate == 0); 12690Sstevel@tonic-gate keysock_consumers[i]->kc_flags |= KC_FLUSHING; 12700Sstevel@tonic-gate mutex_exit(&(keysock_consumers[i]->kc_lock)); 12710Sstevel@tonic-gate /* Always increment the number of flushes... */ 12720Sstevel@tonic-gate keysock_flushdump++; 12730Sstevel@tonic-gate /* Guaranteed to return a message. */ 12740Sstevel@tonic-gate keysock_passdown(ks, mp1, i, extv, B_TRUE); 12750Sstevel@tonic-gate } else if (start == finish) { 12760Sstevel@tonic-gate /* 12770Sstevel@tonic-gate * In case where start == finish, and there's no 12780Sstevel@tonic-gate * consumer, should we force an error? Yes. 12790Sstevel@tonic-gate */ 12800Sstevel@tonic-gate mutex_exit(&keysock_consumers_lock); 12810Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, 12820Sstevel@tonic-gate SADB_X_DIAGNOSTIC_UNKNOWN_SATYPE); 12830Sstevel@tonic-gate return; 12840Sstevel@tonic-gate } 12850Sstevel@tonic-gate } 12860Sstevel@tonic-gate mutex_exit(&keysock_consumers_lock); 12870Sstevel@tonic-gate 12880Sstevel@tonic-gate if (keysock_flushdump == 0) { 12890Sstevel@tonic-gate /* 12900Sstevel@tonic-gate * There were no consumers at all for this message. 12910Sstevel@tonic-gate * XXX For now return ESRCH. 12920Sstevel@tonic-gate */ 12930Sstevel@tonic-gate keysock_error(ks, mp, ESRCH, SADB_X_DIAGNOSTIC_NO_SADBS); 12940Sstevel@tonic-gate } else { 12950Sstevel@tonic-gate /* Otherwise, free the original message. */ 12960Sstevel@tonic-gate freemsg(mp); 12970Sstevel@tonic-gate } 12980Sstevel@tonic-gate } 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate /* 13010Sstevel@tonic-gate * Get the right diagnostic for a duplicate. Should probably use a static 13020Sstevel@tonic-gate * table lookup. 13030Sstevel@tonic-gate */ 13040Sstevel@tonic-gate int 13050Sstevel@tonic-gate keysock_duplicate(int ext_type) 13060Sstevel@tonic-gate { 13070Sstevel@tonic-gate int rc = 0; 13080Sstevel@tonic-gate 13090Sstevel@tonic-gate switch (ext_type) { 13100Sstevel@tonic-gate case SADB_EXT_ADDRESS_SRC: 13110Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_DUPLICATE_SRC; 13120Sstevel@tonic-gate break; 13130Sstevel@tonic-gate case SADB_EXT_ADDRESS_DST: 13140Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_DUPLICATE_DST; 13150Sstevel@tonic-gate break; 13160Sstevel@tonic-gate case SADB_EXT_SA: 13170Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_DUPLICATE_SA; 13180Sstevel@tonic-gate break; 13190Sstevel@tonic-gate case SADB_EXT_SPIRANGE: 13200Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_DUPLICATE_RANGE; 13210Sstevel@tonic-gate break; 13220Sstevel@tonic-gate case SADB_EXT_KEY_AUTH: 13230Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_DUPLICATE_AKEY; 13240Sstevel@tonic-gate break; 13250Sstevel@tonic-gate case SADB_EXT_KEY_ENCRYPT: 13260Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_DUPLICATE_EKEY; 13270Sstevel@tonic-gate break; 13280Sstevel@tonic-gate } 13290Sstevel@tonic-gate return (rc); 13300Sstevel@tonic-gate } 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate /* 13330Sstevel@tonic-gate * Get the right diagnostic for a reality check failure. Should probably use 13340Sstevel@tonic-gate * a static table lookup. 13350Sstevel@tonic-gate */ 13360Sstevel@tonic-gate int 13370Sstevel@tonic-gate keysock_malformed(int ext_type) 13380Sstevel@tonic-gate { 13390Sstevel@tonic-gate int rc = 0; 13400Sstevel@tonic-gate 13410Sstevel@tonic-gate switch (ext_type) { 13420Sstevel@tonic-gate case SADB_EXT_ADDRESS_SRC: 13430Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_MALFORMED_SRC; 13440Sstevel@tonic-gate break; 13450Sstevel@tonic-gate case SADB_EXT_ADDRESS_DST: 13460Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_MALFORMED_DST; 13470Sstevel@tonic-gate break; 13480Sstevel@tonic-gate case SADB_EXT_SA: 13490Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_MALFORMED_SA; 13500Sstevel@tonic-gate break; 13510Sstevel@tonic-gate case SADB_EXT_SPIRANGE: 13520Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_MALFORMED_RANGE; 13530Sstevel@tonic-gate break; 13540Sstevel@tonic-gate case SADB_EXT_KEY_AUTH: 13550Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_MALFORMED_AKEY; 13560Sstevel@tonic-gate break; 13570Sstevel@tonic-gate case SADB_EXT_KEY_ENCRYPT: 13580Sstevel@tonic-gate rc = SADB_X_DIAGNOSTIC_MALFORMED_EKEY; 13590Sstevel@tonic-gate break; 13600Sstevel@tonic-gate } 13610Sstevel@tonic-gate return (rc); 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate /* 13650Sstevel@tonic-gate * Keysock massaging of an inverse ACQUIRE. Consult policy, 13660Sstevel@tonic-gate * and construct an appropriate response. 13670Sstevel@tonic-gate */ 13680Sstevel@tonic-gate static void 13690Sstevel@tonic-gate keysock_inverse_acquire(mblk_t *mp, sadb_msg_t *samsg, sadb_ext_t *extv[], 13700Sstevel@tonic-gate keysock_t *ks) 13710Sstevel@tonic-gate { 13720Sstevel@tonic-gate mblk_t *reply_mp; 13730Sstevel@tonic-gate 13740Sstevel@tonic-gate /* 13750Sstevel@tonic-gate * Reality check things... 13760Sstevel@tonic-gate */ 13770Sstevel@tonic-gate if (extv[SADB_EXT_ADDRESS_SRC] == NULL) { 13780Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_MISSING_SRC); 13790Sstevel@tonic-gate return; 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate if (extv[SADB_EXT_ADDRESS_DST] == NULL) { 13820Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_MISSING_DST); 13830Sstevel@tonic-gate } 13840Sstevel@tonic-gate 13850Sstevel@tonic-gate reply_mp = ipsec_construct_inverse_acquire(samsg, extv); 13860Sstevel@tonic-gate 13870Sstevel@tonic-gate if (reply_mp != NULL) { 13880Sstevel@tonic-gate freemsg(mp); 13890Sstevel@tonic-gate keysock_passup(reply_mp, (sadb_msg_t *)reply_mp->b_rptr, 13900Sstevel@tonic-gate ks->keysock_serial, NULL, B_FALSE); 13910Sstevel@tonic-gate } else { 13920Sstevel@tonic-gate keysock_error(ks, mp, samsg->sadb_msg_errno, 13930Sstevel@tonic-gate samsg->sadb_x_msg_diagnostic); 13940Sstevel@tonic-gate } 13950Sstevel@tonic-gate } 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate /* 13980Sstevel@tonic-gate * Spew an extended REGISTER down to the relevant consumers. 13990Sstevel@tonic-gate */ 14000Sstevel@tonic-gate static void 14010Sstevel@tonic-gate keysock_extended_register(keysock_t *ks, mblk_t *mp, sadb_ext_t *extv[]) 14020Sstevel@tonic-gate { 14030Sstevel@tonic-gate sadb_x_ereg_t *ereg = (sadb_x_ereg_t *)extv[SADB_X_EXT_EREG]; 14040Sstevel@tonic-gate uint8_t *satypes, *fencepost; 14050Sstevel@tonic-gate mblk_t *downmp; 14060Sstevel@tonic-gate sadb_ext_t *downextv[SADB_EXT_MAX + 1]; 14070Sstevel@tonic-gate 14080Sstevel@tonic-gate if (ks->keysock_registered[0] != 0 || ks->keysock_registered[1] != 0 || 14090Sstevel@tonic-gate ks->keysock_registered[2] != 0 || ks->keysock_registered[3] != 0) { 14100Sstevel@tonic-gate keysock_error(ks, mp, EBUSY, 0); 14110Sstevel@tonic-gate } 14120Sstevel@tonic-gate 14130Sstevel@tonic-gate ks->keysock_flags |= KEYSOCK_EXTENDED; 14140Sstevel@tonic-gate if (ereg == NULL) { 14150Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_SATYPE_NEEDED); 14160Sstevel@tonic-gate } else { 14170Sstevel@tonic-gate ASSERT(mp->b_rptr + msgdsize(mp) == mp->b_wptr); 14180Sstevel@tonic-gate fencepost = (uint8_t *)mp->b_wptr; 14190Sstevel@tonic-gate satypes = ereg->sadb_x_ereg_satypes; 14200Sstevel@tonic-gate while (*satypes != SADB_SATYPE_UNSPEC && satypes != fencepost) { 14210Sstevel@tonic-gate downmp = copymsg(mp); 14220Sstevel@tonic-gate if (downmp == NULL) { 14230Sstevel@tonic-gate keysock_error(ks, mp, ENOMEM, 0); 14240Sstevel@tonic-gate return; 14250Sstevel@tonic-gate } 14260Sstevel@tonic-gate /* 14270Sstevel@tonic-gate * Since we've made it here, keysock_get_ext will work! 14280Sstevel@tonic-gate */ 14290Sstevel@tonic-gate (void) keysock_get_ext(downextv, 14300Sstevel@tonic-gate (sadb_msg_t *)downmp->b_rptr, msgdsize(downmp)); 14310Sstevel@tonic-gate keysock_passdown(ks, downmp, *satypes, downextv, 14320Sstevel@tonic-gate B_FALSE); 14330Sstevel@tonic-gate ++satypes; 14340Sstevel@tonic-gate } 14350Sstevel@tonic-gate freemsg(mp); 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 14380Sstevel@tonic-gate /* 14390Sstevel@tonic-gate * Set global to indicate we prefer an extended ACQUIRE. 14400Sstevel@tonic-gate */ 14410Sstevel@tonic-gate atomic_add_32(&keysock_num_extended, 1); 14420Sstevel@tonic-gate } 14430Sstevel@tonic-gate 14440Sstevel@tonic-gate /* 14450Sstevel@tonic-gate * Handle PF_KEY messages. 14460Sstevel@tonic-gate */ 14470Sstevel@tonic-gate static void 14480Sstevel@tonic-gate keysock_parse(queue_t *q, mblk_t *mp) 14490Sstevel@tonic-gate { 14500Sstevel@tonic-gate sadb_msg_t *samsg; 14510Sstevel@tonic-gate sadb_ext_t *extv[SADB_EXT_MAX + 1]; 14520Sstevel@tonic-gate keysock_t *ks = (keysock_t *)q->q_ptr; 14530Sstevel@tonic-gate uint_t msgsize; 14540Sstevel@tonic-gate uint8_t satype; 14550Sstevel@tonic-gate 14560Sstevel@tonic-gate /* Make sure I'm a PF_KEY socket. (i.e. nothing's below me) */ 14570Sstevel@tonic-gate ASSERT(WR(q)->q_next == NULL); 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate samsg = (sadb_msg_t *)mp->b_rptr; 14600Sstevel@tonic-gate ks2dbg(("Received possible PF_KEY message, type %d.\n", 14610Sstevel@tonic-gate samsg->sadb_msg_type)); 14620Sstevel@tonic-gate 14630Sstevel@tonic-gate msgsize = SADB_64TO8(samsg->sadb_msg_len); 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate if (msgdsize(mp) != msgsize) { 14660Sstevel@tonic-gate /* 14670Sstevel@tonic-gate * Message len incorrect w.r.t. actual size. Send an error 14680Sstevel@tonic-gate * (EMSGSIZE). It may be necessary to massage things a 14690Sstevel@tonic-gate * bit. For example, if the sadb_msg_type is hosed, 14700Sstevel@tonic-gate * I need to set it to SADB_RESERVED to get delivery to 14710Sstevel@tonic-gate * do the right thing. Then again, maybe just letting 14720Sstevel@tonic-gate * the error delivery do the right thing. 14730Sstevel@tonic-gate */ 14740Sstevel@tonic-gate ks2dbg(("mblk (%lu) and base (%d) message sizes don't jibe.\n", 14750Sstevel@tonic-gate msgdsize(mp), msgsize)); 14760Sstevel@tonic-gate keysock_error(ks, mp, EMSGSIZE, SADB_X_DIAGNOSTIC_NONE); 14770Sstevel@tonic-gate return; 14780Sstevel@tonic-gate } 14790Sstevel@tonic-gate 14800Sstevel@tonic-gate if (msgsize > (uint_t)(mp->b_wptr - mp->b_rptr)) { 14810Sstevel@tonic-gate /* Get all message into one mblk. */ 14820Sstevel@tonic-gate if (pullupmsg(mp, -1) == 0) { 14830Sstevel@tonic-gate /* 14840Sstevel@tonic-gate * Something screwy happened. 14850Sstevel@tonic-gate */ 14860Sstevel@tonic-gate ks3dbg(("keysock_parse: pullupmsg() failed.\n")); 14870Sstevel@tonic-gate return; 14880Sstevel@tonic-gate } else { 14890Sstevel@tonic-gate samsg = (sadb_msg_t *)mp->b_rptr; 14900Sstevel@tonic-gate } 14910Sstevel@tonic-gate } 14920Sstevel@tonic-gate 14930Sstevel@tonic-gate switch (keysock_get_ext(extv, samsg, msgsize)) { 14940Sstevel@tonic-gate case KGE_DUP: 14950Sstevel@tonic-gate /* Handle duplicate extension. */ 14960Sstevel@tonic-gate ks1dbg(("Got duplicate extension of type %d.\n", 14970Sstevel@tonic-gate extv[0]->sadb_ext_type)); 14980Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, 14990Sstevel@tonic-gate keysock_duplicate(extv[0]->sadb_ext_type)); 15000Sstevel@tonic-gate return; 15010Sstevel@tonic-gate case KGE_UNK: 15020Sstevel@tonic-gate /* Handle unknown extension. */ 15030Sstevel@tonic-gate ks1dbg(("Got unknown extension of type %d.\n", 15040Sstevel@tonic-gate extv[0]->sadb_ext_type)); 15050Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_EXT); 15060Sstevel@tonic-gate return; 15070Sstevel@tonic-gate case KGE_LEN: 15080Sstevel@tonic-gate /* Length error. */ 15090Sstevel@tonic-gate ks1dbg(("Length %d on extension type %d overrun or 0.\n", 15100Sstevel@tonic-gate extv[0]->sadb_ext_len, extv[0]->sadb_ext_type)); 15110Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_BAD_EXTLEN); 15120Sstevel@tonic-gate return; 15130Sstevel@tonic-gate case KGE_CHK: 15140Sstevel@tonic-gate /* Reality check failed. */ 15150Sstevel@tonic-gate ks1dbg(("Reality check failed on extension type %d.\n", 15160Sstevel@tonic-gate extv[0]->sadb_ext_type)); 15170Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, 15180Sstevel@tonic-gate keysock_malformed(extv[0]->sadb_ext_type)); 15190Sstevel@tonic-gate return; 15200Sstevel@tonic-gate default: 15210Sstevel@tonic-gate /* Default case is no errors. */ 15220Sstevel@tonic-gate break; 15230Sstevel@tonic-gate } 15240Sstevel@tonic-gate 15250Sstevel@tonic-gate switch (samsg->sadb_msg_type) { 15260Sstevel@tonic-gate case SADB_REGISTER: 15270Sstevel@tonic-gate /* 15280Sstevel@tonic-gate * There's a semantic weirdness in that a message OTHER than 15290Sstevel@tonic-gate * the return REGISTER message may be passed up if I set the 15300Sstevel@tonic-gate * registered bit BEFORE I pass it down. 15310Sstevel@tonic-gate * 15320Sstevel@tonic-gate * SOOOO, I'll not twiddle any registered bits until I see 15330Sstevel@tonic-gate * the upbound REGISTER (with a serial number in it). 15340Sstevel@tonic-gate */ 15350Sstevel@tonic-gate if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) { 15360Sstevel@tonic-gate /* Handle extended register here. */ 15370Sstevel@tonic-gate keysock_extended_register(ks, mp, extv); 15380Sstevel@tonic-gate return; 15390Sstevel@tonic-gate } else if (ks->keysock_flags & KEYSOCK_EXTENDED) { 15400Sstevel@tonic-gate keysock_error(ks, mp, EBUSY, 0); 15410Sstevel@tonic-gate return; 15420Sstevel@tonic-gate } 15430Sstevel@tonic-gate /* FALLTHRU */ 15440Sstevel@tonic-gate case SADB_GETSPI: 15450Sstevel@tonic-gate case SADB_ADD: 15460Sstevel@tonic-gate case SADB_UPDATE: 15470Sstevel@tonic-gate case SADB_DELETE: 15480Sstevel@tonic-gate case SADB_GET: 15490Sstevel@tonic-gate /* 15500Sstevel@tonic-gate * Pass down to appropriate consumer. 15510Sstevel@tonic-gate */ 15520Sstevel@tonic-gate if (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC) 15530Sstevel@tonic-gate keysock_passdown(ks, mp, samsg->sadb_msg_satype, extv, 15540Sstevel@tonic-gate B_FALSE); 15550Sstevel@tonic-gate else keysock_error(ks, mp, EINVAL, 15560Sstevel@tonic-gate SADB_X_DIAGNOSTIC_SATYPE_NEEDED); 15570Sstevel@tonic-gate return; 15580Sstevel@tonic-gate case SADB_ACQUIRE: 15590Sstevel@tonic-gate /* 15600Sstevel@tonic-gate * If I _receive_ an acquire, this means I should spread it 15610Sstevel@tonic-gate * out to registered sockets. Unless there's an errno... 15620Sstevel@tonic-gate * 15630Sstevel@tonic-gate * Need ADDRESS, may have ID, SENS, and PROP, unless errno, 15640Sstevel@tonic-gate * in which case there should be NO extensions. 15650Sstevel@tonic-gate * 15660Sstevel@tonic-gate * Return to registered. 15670Sstevel@tonic-gate */ 15680Sstevel@tonic-gate if (samsg->sadb_msg_errno != 0) { 15690Sstevel@tonic-gate satype = samsg->sadb_msg_satype; 15700Sstevel@tonic-gate if (satype == SADB_SATYPE_UNSPEC) { 15710Sstevel@tonic-gate if (!(ks->keysock_flags & KEYSOCK_EXTENDED)) { 15720Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, 15730Sstevel@tonic-gate SADB_X_DIAGNOSTIC_SATYPE_NEEDED); 15740Sstevel@tonic-gate return; 15750Sstevel@tonic-gate } 15760Sstevel@tonic-gate /* 15770Sstevel@tonic-gate * Reassign satype based on the first 15780Sstevel@tonic-gate * flags that KEYSOCK_SETREG says. 15790Sstevel@tonic-gate */ 15800Sstevel@tonic-gate while (satype <= SADB_SATYPE_MAX) { 15810Sstevel@tonic-gate if (KEYSOCK_ISREG(ks, satype)) 15820Sstevel@tonic-gate break; 15830Sstevel@tonic-gate satype++; 15840Sstevel@tonic-gate } 15850Sstevel@tonic-gate if (satype > SADB_SATYPE_MAX) { 15860Sstevel@tonic-gate keysock_error(ks, mp, EBUSY, 0); 15870Sstevel@tonic-gate return; 15880Sstevel@tonic-gate } 15890Sstevel@tonic-gate } 15900Sstevel@tonic-gate keysock_passdown(ks, mp, satype, extv, B_FALSE); 15910Sstevel@tonic-gate } else { 15920Sstevel@tonic-gate if (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) 15930Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, 15940Sstevel@tonic-gate SADB_X_DIAGNOSTIC_SATYPE_NEEDED); 15950Sstevel@tonic-gate else 15960Sstevel@tonic-gate keysock_passup(mp, samsg, 0, NULL, B_FALSE); 15970Sstevel@tonic-gate } 15980Sstevel@tonic-gate return; 15990Sstevel@tonic-gate case SADB_EXPIRE: 16000Sstevel@tonic-gate /* 16010Sstevel@tonic-gate * If someone sends this in, then send out to all senders. 16020Sstevel@tonic-gate * (Save maybe ESP or AH, I have to be careful here.) 16030Sstevel@tonic-gate * 16040Sstevel@tonic-gate * Need ADDRESS, may have ID and SENS. 16050Sstevel@tonic-gate * 16060Sstevel@tonic-gate * XXX for now this is unsupported. 16070Sstevel@tonic-gate */ 16080Sstevel@tonic-gate break; 16090Sstevel@tonic-gate case SADB_FLUSH: 16100Sstevel@tonic-gate case SADB_DUMP: /* not used by normal applications */ 16110Sstevel@tonic-gate /* 16120Sstevel@tonic-gate * Nuke all SAs, or dump out the whole SA table to sender only. 16130Sstevel@tonic-gate * 16140Sstevel@tonic-gate * No extensions at all. Return to all listeners. 16150Sstevel@tonic-gate * 16160Sstevel@tonic-gate * Question: Should I hold a lock here to prevent 16170Sstevel@tonic-gate * additions/deletions while flushing? 16180Sstevel@tonic-gate * Answer: No. (See keysock_passdown() for details.) 16190Sstevel@tonic-gate */ 16200Sstevel@tonic-gate if (extv[0] != NULL) { 16210Sstevel@tonic-gate /* 16220Sstevel@tonic-gate * FLUSH or DUMP messages shouldn't have extensions. 16230Sstevel@tonic-gate * Return EINVAL. 16240Sstevel@tonic-gate */ 16250Sstevel@tonic-gate ks2dbg(("FLUSH message with extension.\n")); 16260Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_NO_EXT); 16270Sstevel@tonic-gate return; 16280Sstevel@tonic-gate } 16290Sstevel@tonic-gate 16300Sstevel@tonic-gate /* Passing down of DUMP/FLUSH messages are special. */ 16310Sstevel@tonic-gate qwriter(q, mp, keysock_do_flushdump, PERIM_INNER); 16320Sstevel@tonic-gate return; 16330Sstevel@tonic-gate case SADB_X_PROMISC: 16340Sstevel@tonic-gate /* 16350Sstevel@tonic-gate * Promiscuous processing message. 16360Sstevel@tonic-gate */ 16370Sstevel@tonic-gate if (samsg->sadb_msg_satype == 0) 16380Sstevel@tonic-gate ks->keysock_flags &= ~KEYSOCK_PROMISC; 16390Sstevel@tonic-gate else 16400Sstevel@tonic-gate ks->keysock_flags |= KEYSOCK_PROMISC; 16410Sstevel@tonic-gate keysock_passup(mp, samsg, ks->keysock_serial, NULL, B_FALSE); 16420Sstevel@tonic-gate return; 16430Sstevel@tonic-gate case SADB_X_INVERSE_ACQUIRE: 16440Sstevel@tonic-gate keysock_inverse_acquire(mp, samsg, extv, ks); 16450Sstevel@tonic-gate return; 16460Sstevel@tonic-gate default: 16470Sstevel@tonic-gate ks2dbg(("Got unknown message type %d.\n", 16480Sstevel@tonic-gate samsg->sadb_msg_type)); 16490Sstevel@tonic-gate keysock_error(ks, mp, EINVAL, SADB_X_DIAGNOSTIC_UNKNOWN_MSG); 16500Sstevel@tonic-gate return; 16510Sstevel@tonic-gate } 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate /* As a placeholder... */ 16540Sstevel@tonic-gate ks0dbg(("keysock_parse(): Hit EOPNOTSUPP\n")); 16550Sstevel@tonic-gate keysock_error(ks, mp, EOPNOTSUPP, SADB_X_DIAGNOSTIC_NONE); 16560Sstevel@tonic-gate } 16570Sstevel@tonic-gate 16580Sstevel@tonic-gate /* 16590Sstevel@tonic-gate * wput routing for PF_KEY/keysock/whatever. Unlike the routing socket, 16600Sstevel@tonic-gate * I don't convert to ioctl()'s for IP. I am the end-all driver as far 16610Sstevel@tonic-gate * as PF_KEY sockets are concerned. I do some conversion, but not as much 16620Sstevel@tonic-gate * as IP/rts does. 16630Sstevel@tonic-gate */ 16640Sstevel@tonic-gate static void 16650Sstevel@tonic-gate keysock_wput(queue_t *q, mblk_t *mp) 16660Sstevel@tonic-gate { 16670Sstevel@tonic-gate uchar_t *rptr = mp->b_rptr; 16680Sstevel@tonic-gate mblk_t *mp1; 16690Sstevel@tonic-gate 16700Sstevel@tonic-gate ks3dbg(("In keysock_wput\n")); 16710Sstevel@tonic-gate 16720Sstevel@tonic-gate if (WR(q)->q_next) { 16730Sstevel@tonic-gate keysock_consumer_t *kc = (keysock_consumer_t *)q->q_ptr; 16740Sstevel@tonic-gate 16750Sstevel@tonic-gate /* 16760Sstevel@tonic-gate * We shouldn't get writes on a consumer instance. 16770Sstevel@tonic-gate * But for now, just passthru. 16780Sstevel@tonic-gate */ 16790Sstevel@tonic-gate ks1dbg(("Huh? wput for an consumer instance (%d)?\n", 16800Sstevel@tonic-gate kc->kc_sa_type)); 16810Sstevel@tonic-gate putnext(q, mp); 16820Sstevel@tonic-gate return; 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate 16850Sstevel@tonic-gate switch (mp->b_datap->db_type) { 16860Sstevel@tonic-gate case M_DATA: 16870Sstevel@tonic-gate /* 16880Sstevel@tonic-gate * Silently discard. 16890Sstevel@tonic-gate */ 16900Sstevel@tonic-gate ks2dbg(("raw M_DATA in keysock.\n")); 16910Sstevel@tonic-gate freemsg(mp); 16920Sstevel@tonic-gate return; 16930Sstevel@tonic-gate case M_PROTO: 16940Sstevel@tonic-gate case M_PCPROTO: 16950Sstevel@tonic-gate if ((mp->b_wptr - rptr) >= sizeof (struct T_data_req)) { 16960Sstevel@tonic-gate if (((union T_primitives *)rptr)->type == T_DATA_REQ) { 16970Sstevel@tonic-gate if ((mp1 = mp->b_cont) == NULL) { 16980Sstevel@tonic-gate /* No data after T_DATA_REQ. */ 16990Sstevel@tonic-gate ks2dbg(("No data after DATA_REQ.\n")); 17000Sstevel@tonic-gate freemsg(mp); 17010Sstevel@tonic-gate return; 17020Sstevel@tonic-gate } 17030Sstevel@tonic-gate freeb(mp); 17040Sstevel@tonic-gate mp = mp1; 17050Sstevel@tonic-gate ks2dbg(("T_DATA_REQ\n")); 17060Sstevel@tonic-gate break; /* Out of switch. */ 17070Sstevel@tonic-gate } 17080Sstevel@tonic-gate } 17090Sstevel@tonic-gate /* FALLTHRU */ 17100Sstevel@tonic-gate default: 17110Sstevel@tonic-gate ks3dbg(("In default wput case (%d %d).\n", 17120Sstevel@tonic-gate mp->b_datap->db_type, ((union T_primitives *)rptr)->type)); 17130Sstevel@tonic-gate keysock_wput_other(q, mp); 17140Sstevel@tonic-gate return; 17150Sstevel@tonic-gate } 17160Sstevel@tonic-gate 17170Sstevel@tonic-gate /* I now have a PF_KEY message in an M_DATA block, pointed to by mp. */ 17180Sstevel@tonic-gate keysock_parse(q, mp); 17190Sstevel@tonic-gate } 17200Sstevel@tonic-gate 17210Sstevel@tonic-gate /* BELOW THIS LINE ARE ROUTINES INCLUDING AND RELATED TO keysock_rput(). */ 17220Sstevel@tonic-gate 17230Sstevel@tonic-gate /* 17240Sstevel@tonic-gate * Called upon receipt of a KEYSOCK_HELLO_ACK to set up the appropriate 17250Sstevel@tonic-gate * state vectors. 17260Sstevel@tonic-gate */ 17270Sstevel@tonic-gate static void 17280Sstevel@tonic-gate keysock_link_consumer(uint8_t satype, keysock_consumer_t *kc) 17290Sstevel@tonic-gate { 17300Sstevel@tonic-gate keysock_t *ks; 17310Sstevel@tonic-gate 17320Sstevel@tonic-gate mutex_enter(&keysock_consumers_lock); 17330Sstevel@tonic-gate mutex_enter(&kc->kc_lock); 17340Sstevel@tonic-gate if (keysock_consumers[satype] != NULL) { 17350Sstevel@tonic-gate ks0dbg(( 17360Sstevel@tonic-gate "Hmmmm, someone closed %d before the HELLO_ACK happened.\n", 17370Sstevel@tonic-gate satype)); 17380Sstevel@tonic-gate /* 17390Sstevel@tonic-gate * Perhaps updating the new below-me consumer with what I have 17400Sstevel@tonic-gate * so far would work too? 17410Sstevel@tonic-gate */ 17420Sstevel@tonic-gate mutex_exit(&kc->kc_lock); 17430Sstevel@tonic-gate mutex_exit(&keysock_consumers_lock); 17440Sstevel@tonic-gate } else { 17450Sstevel@tonic-gate /* Add new below-me consumer. */ 17460Sstevel@tonic-gate keysock_consumers[satype] = kc; 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate kc->kc_flags = 0; 17490Sstevel@tonic-gate kc->kc_sa_type = satype; 17500Sstevel@tonic-gate mutex_exit(&kc->kc_lock); 17510Sstevel@tonic-gate mutex_exit(&keysock_consumers_lock); 17520Sstevel@tonic-gate 17530Sstevel@tonic-gate /* Scan the keysock list. */ 17540Sstevel@tonic-gate mutex_enter(&keysock_list_lock); 17550Sstevel@tonic-gate for (ks = keysock_list; ks != NULL; ks = ks->keysock_next) { 17560Sstevel@tonic-gate if (KEYSOCK_ISREG(ks, satype)) { 17570Sstevel@tonic-gate /* 17580Sstevel@tonic-gate * XXX Perhaps send an SADB_REGISTER down on 17590Sstevel@tonic-gate * the socket's behalf. 17600Sstevel@tonic-gate */ 17610Sstevel@tonic-gate ks1dbg(("Socket %u registered already for " 17620Sstevel@tonic-gate "new consumer.\n", ks->keysock_serial)); 17630Sstevel@tonic-gate } 17640Sstevel@tonic-gate } 17650Sstevel@tonic-gate mutex_exit(&keysock_list_lock); 17660Sstevel@tonic-gate } 17670Sstevel@tonic-gate } 17680Sstevel@tonic-gate 17690Sstevel@tonic-gate /* 17700Sstevel@tonic-gate * Generate a KEYSOCK_OUT_ERR message for my consumer. 17710Sstevel@tonic-gate */ 17720Sstevel@tonic-gate static void 17730Sstevel@tonic-gate keysock_out_err(keysock_consumer_t *kc, int ks_errno, mblk_t *mp) 17740Sstevel@tonic-gate { 17750Sstevel@tonic-gate keysock_out_err_t *kse; 17760Sstevel@tonic-gate mblk_t *imp; 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate imp = allocb(sizeof (ipsec_info_t), BPRI_HI); 17790Sstevel@tonic-gate if (imp == NULL) { 17800Sstevel@tonic-gate ks1dbg(("keysock_out_err: Can't alloc message.\n")); 17810Sstevel@tonic-gate return; 17820Sstevel@tonic-gate } 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate imp->b_datap->db_type = M_CTL; 17850Sstevel@tonic-gate imp->b_wptr += sizeof (ipsec_info_t); 17860Sstevel@tonic-gate 17870Sstevel@tonic-gate kse = (keysock_out_err_t *)imp->b_rptr; 17880Sstevel@tonic-gate imp->b_cont = mp; 17890Sstevel@tonic-gate kse->ks_err_type = KEYSOCK_OUT_ERR; 17900Sstevel@tonic-gate kse->ks_err_len = sizeof (*kse); 17910Sstevel@tonic-gate /* Is serial necessary? */ 17920Sstevel@tonic-gate kse->ks_err_serial = 0; 17930Sstevel@tonic-gate kse->ks_err_errno = ks_errno; 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate /* 17960Sstevel@tonic-gate * XXX What else do I need to do here w.r.t. information 17970Sstevel@tonic-gate * to tell the consumer what caused this error? 17980Sstevel@tonic-gate * 17990Sstevel@tonic-gate * I believe the answer is the PF_KEY ACQUIRE (or other) message 18000Sstevel@tonic-gate * attached in mp, which is appended at the end. I believe the 18010Sstevel@tonic-gate * db_ref won't matter here, because the PF_KEY message is only read 18020Sstevel@tonic-gate * for KEYSOCK_OUT_ERR. 18030Sstevel@tonic-gate */ 18040Sstevel@tonic-gate 18050Sstevel@tonic-gate putnext(kc->kc_wq, imp); 18060Sstevel@tonic-gate } 18070Sstevel@tonic-gate 18080Sstevel@tonic-gate /* XXX this is a hack errno. */ 18090Sstevel@tonic-gate #define EIPSECNOSA 255 18100Sstevel@tonic-gate 18110Sstevel@tonic-gate /* 18120Sstevel@tonic-gate * Route message (pointed by mp, header in samsg) toward appropriate 18130Sstevel@tonic-gate * sockets. Assume the message's creator did its job correctly. 18140Sstevel@tonic-gate * 18150Sstevel@tonic-gate * This should be a function that is followed by a return in its caller. 18160Sstevel@tonic-gate * The compiler _should_ be able to use tail-call optimizations to make the 18170Sstevel@tonic-gate * large ## of parameters not a huge deal. 18180Sstevel@tonic-gate */ 18190Sstevel@tonic-gate static void 18200Sstevel@tonic-gate keysock_passup(mblk_t *mp, sadb_msg_t *samsg, minor_t serial, 18210Sstevel@tonic-gate keysock_consumer_t *kc, boolean_t persistent) 18220Sstevel@tonic-gate { 18230Sstevel@tonic-gate keysock_t *ks; 18240Sstevel@tonic-gate uint8_t satype = samsg->sadb_msg_satype; 18250Sstevel@tonic-gate boolean_t toall = B_FALSE, allreg = B_FALSE, allereg = B_FALSE, 18260Sstevel@tonic-gate setalg = B_FALSE; 18270Sstevel@tonic-gate mblk_t *mp1; 18280Sstevel@tonic-gate int err = EIPSECNOSA; 18290Sstevel@tonic-gate 18300Sstevel@tonic-gate /* Convert mp, which is M_DATA, into an M_PROTO of type T_DATA_IND */ 18310Sstevel@tonic-gate mp1 = allocb(sizeof (struct T_data_req), BPRI_HI); 18320Sstevel@tonic-gate if (mp1 == NULL) { 18330Sstevel@tonic-gate err = ENOMEM; 18340Sstevel@tonic-gate goto error; 18350Sstevel@tonic-gate } 18360Sstevel@tonic-gate mp1->b_wptr += sizeof (struct T_data_req); 18370Sstevel@tonic-gate ((struct T_data_ind *)mp1->b_rptr)->PRIM_type = T_DATA_IND; 18380Sstevel@tonic-gate ((struct T_data_ind *)mp1->b_rptr)->MORE_flag = 0; 18390Sstevel@tonic-gate mp1->b_datap->db_type = M_PROTO; 18400Sstevel@tonic-gate mp1->b_cont = mp; 18410Sstevel@tonic-gate mp = mp1; 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate switch (samsg->sadb_msg_type) { 18440Sstevel@tonic-gate case SADB_FLUSH: 18450Sstevel@tonic-gate case SADB_GETSPI: 18460Sstevel@tonic-gate case SADB_UPDATE: 18470Sstevel@tonic-gate case SADB_ADD: 18480Sstevel@tonic-gate case SADB_DELETE: 18490Sstevel@tonic-gate case SADB_EXPIRE: 18500Sstevel@tonic-gate /* 18510Sstevel@tonic-gate * These are most likely replies. Don't worry about 18520Sstevel@tonic-gate * KEYSOCK_OUT_ERR handling. Deliver to all sockets. 18530Sstevel@tonic-gate */ 18540Sstevel@tonic-gate ks3dbg(("Delivering normal message (%d) to all sockets.\n", 18550Sstevel@tonic-gate samsg->sadb_msg_type)); 18560Sstevel@tonic-gate toall = B_TRUE; 18570Sstevel@tonic-gate break; 18580Sstevel@tonic-gate case SADB_REGISTER: 18590Sstevel@tonic-gate /* 18600Sstevel@tonic-gate * REGISTERs come up for one of three reasons: 18610Sstevel@tonic-gate * 18620Sstevel@tonic-gate * 1.) In response to a normal SADB_REGISTER 18630Sstevel@tonic-gate * (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC && 18640Sstevel@tonic-gate * serial != 0) 18650Sstevel@tonic-gate * Deliver to normal SADB_REGISTERed sockets. 18660Sstevel@tonic-gate * 2.) In response to an extended REGISTER 18670Sstevel@tonic-gate * (samsg->sadb_msg_satype == SADB_SATYPE_UNSPEC) 18680Sstevel@tonic-gate * Deliver to extended REGISTERed socket. 18690Sstevel@tonic-gate * 3.) Spontaneous algorithm changes 18700Sstevel@tonic-gate * (samsg->sadb_msg_satype != SADB_SATYPE_UNSPEC && 18710Sstevel@tonic-gate * serial == 0) 18720Sstevel@tonic-gate * Deliver to REGISTERed sockets of all sorts. 18730Sstevel@tonic-gate */ 18740Sstevel@tonic-gate if (kc == NULL) { 18750Sstevel@tonic-gate /* Here because of keysock_error() call. */ 18760Sstevel@tonic-gate ASSERT(samsg->sadb_msg_errno != 0); 18770Sstevel@tonic-gate break; /* Out of switch. */ 18780Sstevel@tonic-gate } 18790Sstevel@tonic-gate ks3dbg(("Delivering REGISTER.\n")); 18800Sstevel@tonic-gate if (satype == SADB_SATYPE_UNSPEC) { 18810Sstevel@tonic-gate /* REGISTER Reason #2 */ 18820Sstevel@tonic-gate allereg = B_TRUE; 18830Sstevel@tonic-gate /* 18840Sstevel@tonic-gate * Rewhack SA type so PF_KEY socket holder knows what 18850Sstevel@tonic-gate * consumer generated this algorithm list. 18860Sstevel@tonic-gate */ 18870Sstevel@tonic-gate satype = kc->kc_sa_type; 18880Sstevel@tonic-gate samsg->sadb_msg_satype = satype; 18890Sstevel@tonic-gate setalg = B_TRUE; 18900Sstevel@tonic-gate } else if (serial == 0) { 18910Sstevel@tonic-gate /* REGISTER Reason #3 */ 18920Sstevel@tonic-gate allreg = B_TRUE; 18930Sstevel@tonic-gate allereg = B_TRUE; 18940Sstevel@tonic-gate } else { 18950Sstevel@tonic-gate /* REGISTER Reason #1 */ 18960Sstevel@tonic-gate allreg = B_TRUE; 18970Sstevel@tonic-gate setalg = B_TRUE; 18980Sstevel@tonic-gate } 18990Sstevel@tonic-gate break; 19000Sstevel@tonic-gate case SADB_ACQUIRE: 19010Sstevel@tonic-gate /* 19020Sstevel@tonic-gate * ACQUIREs are either extended (sadb_msg_satype == 0) or 19030Sstevel@tonic-gate * regular (sadb_msg_satype != 0). And we're guaranteed 19040Sstevel@tonic-gate * that serial == 0 for an ACQUIRE. 19050Sstevel@tonic-gate */ 19060Sstevel@tonic-gate ks3dbg(("Delivering ACQUIRE.\n")); 19070Sstevel@tonic-gate allereg = (satype == SADB_SATYPE_UNSPEC); 19080Sstevel@tonic-gate allreg = !allereg; 19090Sstevel@tonic-gate /* 19100Sstevel@tonic-gate * Corner case - if we send a regular ACQUIRE and there's 19110Sstevel@tonic-gate * extended ones registered, don't send an error down to 19120Sstevel@tonic-gate * consumers if nobody's listening and prematurely destroy 19130Sstevel@tonic-gate * their ACQUIRE record. This might be too hackish of a 19140Sstevel@tonic-gate * solution. 19150Sstevel@tonic-gate */ 19160Sstevel@tonic-gate if (allreg && keysock_num_extended > 0) 19170Sstevel@tonic-gate err = 0; 19180Sstevel@tonic-gate break; 19190Sstevel@tonic-gate case SADB_X_PROMISC: 19200Sstevel@tonic-gate case SADB_X_INVERSE_ACQUIRE: 19210Sstevel@tonic-gate case SADB_DUMP: 19220Sstevel@tonic-gate case SADB_GET: 19230Sstevel@tonic-gate default: 19240Sstevel@tonic-gate /* 19250Sstevel@tonic-gate * Deliver to the sender and promiscuous only. 19260Sstevel@tonic-gate */ 19270Sstevel@tonic-gate ks3dbg(("Delivering sender/promisc only (%d).\n", 19280Sstevel@tonic-gate samsg->sadb_msg_type)); 19290Sstevel@tonic-gate break; 19300Sstevel@tonic-gate } 19310Sstevel@tonic-gate 19320Sstevel@tonic-gate mutex_enter(&keysock_list_lock); 19330Sstevel@tonic-gate for (ks = keysock_list; ks != NULL; ks = ks->keysock_next) { 19340Sstevel@tonic-gate /* Delivery loop. */ 19350Sstevel@tonic-gate 19360Sstevel@tonic-gate /* 19370Sstevel@tonic-gate * Check special keysock-setting cases (REGISTER replies) 19380Sstevel@tonic-gate * here. 19390Sstevel@tonic-gate */ 19400Sstevel@tonic-gate if (setalg && serial == ks->keysock_serial) { 19410Sstevel@tonic-gate ASSERT(kc != NULL); 19420Sstevel@tonic-gate ASSERT(kc->kc_sa_type == satype); 19430Sstevel@tonic-gate KEYSOCK_SETREG(ks, satype); 19440Sstevel@tonic-gate } 19450Sstevel@tonic-gate 19460Sstevel@tonic-gate /* 19470Sstevel@tonic-gate * NOLOOP takes precedence over PROMISC. So if you've set 19480Sstevel@tonic-gate * !SO_USELOOPBACK, don't expect to see any data... 19490Sstevel@tonic-gate */ 19500Sstevel@tonic-gate if (ks->keysock_flags & KEYSOCK_NOLOOP) 19510Sstevel@tonic-gate continue; 19520Sstevel@tonic-gate 19530Sstevel@tonic-gate /* 19540Sstevel@tonic-gate * Messages to all, or promiscuous sockets just GET the 19550Sstevel@tonic-gate * message. Perform rules-type checking iff it's not for all 19560Sstevel@tonic-gate * listeners or the socket is in promiscuous mode. 19570Sstevel@tonic-gate * 19580Sstevel@tonic-gate * NOTE:Because of the (kc != NULL && ISREG()), make sure 19590Sstevel@tonic-gate * extended ACQUIREs arrive off a consumer that is 19600Sstevel@tonic-gate * part of the extended REGISTER set of consumers. 19610Sstevel@tonic-gate */ 19620Sstevel@tonic-gate if (serial != ks->keysock_serial && 19630Sstevel@tonic-gate !toall && 19640Sstevel@tonic-gate !(ks->keysock_flags & KEYSOCK_PROMISC) && 19650Sstevel@tonic-gate !((ks->keysock_flags & KEYSOCK_EXTENDED) ? 19660Sstevel@tonic-gate allereg : allreg && kc != NULL && 19670Sstevel@tonic-gate KEYSOCK_ISREG(ks, kc->kc_sa_type))) 19680Sstevel@tonic-gate continue; 19690Sstevel@tonic-gate 19700Sstevel@tonic-gate mp1 = dupmsg(mp); 19710Sstevel@tonic-gate if (mp1 == NULL) { 19720Sstevel@tonic-gate ks2dbg(( 19730Sstevel@tonic-gate "keysock_passup(): dupmsg() failed.\n")); 19740Sstevel@tonic-gate mp1 = mp; 19750Sstevel@tonic-gate mp = NULL; 19760Sstevel@tonic-gate err = ENOMEM; 19770Sstevel@tonic-gate } 19780Sstevel@tonic-gate 19790Sstevel@tonic-gate /* 19800Sstevel@tonic-gate * At this point, we can deliver or attempt to deliver 19810Sstevel@tonic-gate * this message. We're free of obligation to report 19820Sstevel@tonic-gate * no listening PF_KEY sockets. So set err to 0. 19830Sstevel@tonic-gate */ 19840Sstevel@tonic-gate err = 0; 19850Sstevel@tonic-gate 19860Sstevel@tonic-gate /* 19870Sstevel@tonic-gate * See if we canputnext(), as well as see if the message 19880Sstevel@tonic-gate * needs to be queued if we can't. 19890Sstevel@tonic-gate */ 19900Sstevel@tonic-gate if (!canputnext(ks->keysock_rq)) { 19910Sstevel@tonic-gate if (persistent) { 19920Sstevel@tonic-gate if (putq(ks->keysock_rq, mp1) == 0) { 19930Sstevel@tonic-gate ks1dbg(( 19940Sstevel@tonic-gate "keysock_passup: putq failed.\n")); 19950Sstevel@tonic-gate } else { 19960Sstevel@tonic-gate continue; 19970Sstevel@tonic-gate } 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate freemsg(mp1); 20000Sstevel@tonic-gate continue; 20010Sstevel@tonic-gate } 20020Sstevel@tonic-gate 20030Sstevel@tonic-gate ks3dbg(("Putting to serial %d.\n", ks->keysock_serial)); 20040Sstevel@tonic-gate /* 20050Sstevel@tonic-gate * Unlike the specific keysock instance case, this 20060Sstevel@tonic-gate * will only hit for listeners, so we will only 20070Sstevel@tonic-gate * putnext() if we can. 20080Sstevel@tonic-gate */ 20090Sstevel@tonic-gate putnext(ks->keysock_rq, mp1); 20100Sstevel@tonic-gate if (mp == NULL) 20110Sstevel@tonic-gate break; /* out of for loop. */ 20120Sstevel@tonic-gate } 20130Sstevel@tonic-gate mutex_exit(&keysock_list_lock); 20140Sstevel@tonic-gate 20150Sstevel@tonic-gate error: 20160Sstevel@tonic-gate if ((err != 0) && (kc != NULL)) { 20170Sstevel@tonic-gate /* 20180Sstevel@tonic-gate * Generate KEYSOCK_OUT_ERR for consumer. 20190Sstevel@tonic-gate * Basically, I send this back if I have not been able to 20200Sstevel@tonic-gate * transmit (for whatever reason) 20210Sstevel@tonic-gate */ 20220Sstevel@tonic-gate ks1dbg(("keysock_passup(): No registered of type %d.\n", 20230Sstevel@tonic-gate satype)); 20240Sstevel@tonic-gate if (mp != NULL) { 20250Sstevel@tonic-gate if (mp->b_datap->db_type == M_PROTO) { 20260Sstevel@tonic-gate mp1 = mp; 20270Sstevel@tonic-gate mp = mp->b_cont; 20280Sstevel@tonic-gate freeb(mp1); 20290Sstevel@tonic-gate } 20300Sstevel@tonic-gate /* 20310Sstevel@tonic-gate * Do a copymsg() because people who get 20320Sstevel@tonic-gate * KEYSOCK_OUT_ERR may alter the message contents. 20330Sstevel@tonic-gate */ 20340Sstevel@tonic-gate mp1 = copymsg(mp); 20350Sstevel@tonic-gate if (mp1 == NULL) { 20360Sstevel@tonic-gate ks2dbg(("keysock_passup: copymsg() failed.\n")); 20370Sstevel@tonic-gate mp1 = mp; 20380Sstevel@tonic-gate mp = NULL; 20390Sstevel@tonic-gate } 20400Sstevel@tonic-gate keysock_out_err(kc, err, mp1); 20410Sstevel@tonic-gate } 20420Sstevel@tonic-gate } 20430Sstevel@tonic-gate 20440Sstevel@tonic-gate /* 20450Sstevel@tonic-gate * XXX Blank the message somehow. This is difficult because we don't 20460Sstevel@tonic-gate * know at this point if the message has db_ref > 1, etc. 20470Sstevel@tonic-gate * 20480Sstevel@tonic-gate * Optimally, keysock messages containing actual keying material would 20490Sstevel@tonic-gate * be allocated with esballoc(), with a zeroing free function. 20500Sstevel@tonic-gate */ 20510Sstevel@tonic-gate if (mp != NULL) 20520Sstevel@tonic-gate freemsg(mp); 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate 20550Sstevel@tonic-gate /* 20560Sstevel@tonic-gate * Keysock's read service procedure is there only for PF_KEY reply 20570Sstevel@tonic-gate * messages that really need to reach the top. 20580Sstevel@tonic-gate */ 20590Sstevel@tonic-gate static void 20600Sstevel@tonic-gate keysock_rsrv(queue_t *q) 20610Sstevel@tonic-gate { 20620Sstevel@tonic-gate mblk_t *mp; 20630Sstevel@tonic-gate 20640Sstevel@tonic-gate while ((mp = getq(q)) != NULL) { 20650Sstevel@tonic-gate if (canputnext(q)) { 20660Sstevel@tonic-gate putnext(q, mp); 20670Sstevel@tonic-gate } else { 20680Sstevel@tonic-gate (void) putbq(q, mp); 20690Sstevel@tonic-gate return; 20700Sstevel@tonic-gate } 20710Sstevel@tonic-gate } 20720Sstevel@tonic-gate } 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate /* 20750Sstevel@tonic-gate * The read procedure should only be invoked by a keysock consumer, like 20760Sstevel@tonic-gate * ESP, AH, etc. I should only see KEYSOCK_OUT and KEYSOCK_HELLO_ACK 20770Sstevel@tonic-gate * messages on my read queues. 20780Sstevel@tonic-gate */ 20790Sstevel@tonic-gate static void 20800Sstevel@tonic-gate keysock_rput(queue_t *q, mblk_t *mp) 20810Sstevel@tonic-gate { 20820Sstevel@tonic-gate keysock_consumer_t *kc = (keysock_consumer_t *)q->q_ptr; 20830Sstevel@tonic-gate ipsec_info_t *ii; 20840Sstevel@tonic-gate keysock_hello_ack_t *ksa; 20850Sstevel@tonic-gate minor_t serial; 20860Sstevel@tonic-gate mblk_t *mp1; 20870Sstevel@tonic-gate sadb_msg_t *samsg; 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate /* Make sure I'm a consumer instance. (i.e. something's below me) */ 20900Sstevel@tonic-gate ASSERT(WR(q)->q_next != NULL); 20910Sstevel@tonic-gate 20920Sstevel@tonic-gate if (mp->b_datap->db_type != M_CTL) { 20930Sstevel@tonic-gate /* 20940Sstevel@tonic-gate * Keysock should only see keysock consumer interface 20950Sstevel@tonic-gate * messages (see ipsec_info.h) on its read procedure. 20960Sstevel@tonic-gate * To be robust, however, putnext() up so the STREAM head can 20970Sstevel@tonic-gate * deal with it appropriately. 20980Sstevel@tonic-gate */ 20990Sstevel@tonic-gate ks1dbg(("Hmmm, a non M_CTL (%d, 0x%x) on keysock_rput.\n", 21000Sstevel@tonic-gate mp->b_datap->db_type, mp->b_datap->db_type)); 21010Sstevel@tonic-gate putnext(q, mp); 21020Sstevel@tonic-gate return; 21030Sstevel@tonic-gate } 21040Sstevel@tonic-gate 21050Sstevel@tonic-gate ii = (ipsec_info_t *)mp->b_rptr; 21060Sstevel@tonic-gate 21070Sstevel@tonic-gate switch (ii->ipsec_info_type) { 21080Sstevel@tonic-gate case KEYSOCK_OUT: 21090Sstevel@tonic-gate /* 21100Sstevel@tonic-gate * A consumer needs to pass a response message or an ACQUIRE 21110Sstevel@tonic-gate * UP. I assume that the consumer has done the right 21120Sstevel@tonic-gate * thing w.r.t. message creation, etc. 21130Sstevel@tonic-gate */ 21140Sstevel@tonic-gate serial = ((keysock_out_t *)mp->b_rptr)->ks_out_serial; 21150Sstevel@tonic-gate mp1 = mp->b_cont; /* Get M_DATA portion. */ 21160Sstevel@tonic-gate freeb(mp); 21170Sstevel@tonic-gate samsg = (sadb_msg_t *)mp1->b_rptr; 21180Sstevel@tonic-gate if (samsg->sadb_msg_type == SADB_FLUSH || 21190Sstevel@tonic-gate (samsg->sadb_msg_type == SADB_DUMP && 21200Sstevel@tonic-gate samsg->sadb_msg_len == SADB_8TO64(sizeof (*samsg)))) { 21210Sstevel@tonic-gate /* 21220Sstevel@tonic-gate * If I'm an end-of-FLUSH or an end-of-DUMP marker... 21230Sstevel@tonic-gate */ 21240Sstevel@tonic-gate ASSERT(keysock_flushdump != 0); /* Am I flushing? */ 21250Sstevel@tonic-gate 21260Sstevel@tonic-gate mutex_enter(&kc->kc_lock); 21270Sstevel@tonic-gate kc->kc_flags &= ~KC_FLUSHING; 21280Sstevel@tonic-gate mutex_exit(&kc->kc_lock); 21290Sstevel@tonic-gate 21300Sstevel@tonic-gate if (samsg->sadb_msg_errno != 0) 21310Sstevel@tonic-gate keysock_flushdump_errno = samsg->sadb_msg_errno; 21320Sstevel@tonic-gate 21330Sstevel@tonic-gate /* 21340Sstevel@tonic-gate * Lower the atomic "flushing" count. If it's 21350Sstevel@tonic-gate * the last one, send up the end-of-{FLUSH,DUMP} to 21360Sstevel@tonic-gate * the appropriate PF_KEY socket. 21370Sstevel@tonic-gate */ 21380Sstevel@tonic-gate if (atomic_add_32_nv(&keysock_flushdump, -1) != 0) { 21390Sstevel@tonic-gate ks1dbg(("One flush/dump message back from %d," 21400Sstevel@tonic-gate " more to go.\n", samsg->sadb_msg_satype)); 21410Sstevel@tonic-gate freemsg(mp1); 21420Sstevel@tonic-gate return; 21430Sstevel@tonic-gate } 21440Sstevel@tonic-gate 21450Sstevel@tonic-gate samsg->sadb_msg_errno = 21460Sstevel@tonic-gate (uint8_t)keysock_flushdump_errno; 21470Sstevel@tonic-gate if (samsg->sadb_msg_type == SADB_DUMP) { 21480Sstevel@tonic-gate samsg->sadb_msg_seq = 0; 21490Sstevel@tonic-gate } 21500Sstevel@tonic-gate } 21510Sstevel@tonic-gate keysock_passup(mp1, samsg, serial, kc, 21520Sstevel@tonic-gate (samsg->sadb_msg_type == SADB_DUMP)); 21530Sstevel@tonic-gate return; 21540Sstevel@tonic-gate case KEYSOCK_HELLO_ACK: 21550Sstevel@tonic-gate /* Aha, now we can link in the consumer! */ 21560Sstevel@tonic-gate ksa = (keysock_hello_ack_t *)ii; 21570Sstevel@tonic-gate keysock_link_consumer(ksa->ks_hello_satype, kc); 21580Sstevel@tonic-gate freemsg(mp); 21590Sstevel@tonic-gate return; 21600Sstevel@tonic-gate default: 21610Sstevel@tonic-gate ks1dbg(("Hmmm, an IPsec info I'm not used to, 0x%x\n", 21620Sstevel@tonic-gate ii->ipsec_info_type)); 21630Sstevel@tonic-gate putnext(q, mp); 21640Sstevel@tonic-gate } 21650Sstevel@tonic-gate } 21660Sstevel@tonic-gate 21670Sstevel@tonic-gate /* 21680Sstevel@tonic-gate * So we can avoid external linking problems.... 21690Sstevel@tonic-gate */ 21700Sstevel@tonic-gate boolean_t 21710Sstevel@tonic-gate keysock_extended_reg(void) 21720Sstevel@tonic-gate { 21730Sstevel@tonic-gate return (keysock_num_extended != 0); 21740Sstevel@tonic-gate } 21750Sstevel@tonic-gate 21760Sstevel@tonic-gate uint32_t 21770Sstevel@tonic-gate keysock_next_seq(void) 21780Sstevel@tonic-gate { 21790Sstevel@tonic-gate return (atomic_add_32_nv(&keysock_acquire_seq, -1)); 21800Sstevel@tonic-gate } 2181