xref: /illumos-gate/usr/src/uts/common/inet/keysock.h (revision 1edba515a3484e0f74b638b203d462b3112ac84d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5f4b3ec61Sdh155122  * Common Development and Distribution License (the "License").
6f4b3ec61Sdh155122  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22bd670b35SErik Nordmark  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*1edba515SAndy Fiddaman  *
25*1edba515SAndy Fiddaman  * Copyright 2024 Oxide Computer Company
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #ifndef	_INET_KEYSOCK_H
297c478bd9Sstevel@tonic-gate #define	_INET_KEYSOCK_H
307c478bd9Sstevel@tonic-gate 
31*1edba515SAndy Fiddaman #include <inet/optcom.h>
32*1edba515SAndy Fiddaman #include <net/pfkeyv2.h>
33*1edba515SAndy Fiddaman 
347c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
357c478bd9Sstevel@tonic-gate extern "C" {
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * Object to represent database of options to search passed to
407c478bd9Sstevel@tonic-gate  * {sock,tpi}optcom_req() interface routine to take care of option
417c478bd9Sstevel@tonic-gate  * management and associated methods.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate extern optdb_obj_t	keysock_opt_obj;
457c478bd9Sstevel@tonic-gate extern uint_t		keysock_max_optsize;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
48f4b3ec61Sdh155122  * KEYSOCK stack instances
49f4b3ec61Sdh155122  */
50f4b3ec61Sdh155122 struct keysock_stack {
51f4b3ec61Sdh155122 	netstack_t		*keystack_netstack;	/* Common netstack */
52f4b3ec61Sdh155122 	/*
53f4b3ec61Sdh155122 	 * keysock_plumbed: zero if plumb not attempted, positive if it
54f4b3ec61Sdh155122 	 * succeeded,  negative if it failed.
55f4b3ec61Sdh155122 	 */
56f4b3ec61Sdh155122 	int			keystack_plumbed;
57f4b3ec61Sdh155122 	caddr_t			keystack_g_nd;
58f4b3ec61Sdh155122 	struct keysockparam_s	*keystack_params;
59f4b3ec61Sdh155122 
60f4b3ec61Sdh155122 	kmutex_t		keystack_param_lock;
61f4b3ec61Sdh155122 				/* Protects the NDD variables. */
62f4b3ec61Sdh155122 
63f4b3ec61Sdh155122 	/* List of open PF_KEY sockets, protected by keysock_list_lock. */
64f4b3ec61Sdh155122 	kmutex_t		keystack_list_lock;
65f4b3ec61Sdh155122 	struct keysock_s	*keystack_list;
66f4b3ec61Sdh155122 
67f4b3ec61Sdh155122 	/*
68f4b3ec61Sdh155122 	 * Consumers table. If an entry is NULL, keysock maintains
69f4b3ec61Sdh155122 	 * the table.
70f4b3ec61Sdh155122 	 */
71f4b3ec61Sdh155122 	kmutex_t		keystack_consumers_lock;
72f4b3ec61Sdh155122 
73f4b3ec61Sdh155122 #define	KEYSOCK_MAX_CONSUMERS 256
74f4b3ec61Sdh155122 	struct keysock_consumer_s *keystack_consumers[KEYSOCK_MAX_CONSUMERS];
75f4b3ec61Sdh155122 
76f4b3ec61Sdh155122 	/*
77f4b3ec61Sdh155122 	 * State for flush/dump.  This would normally be a boolean_t, but
7875d94465SJosef 'Jeff' Sipek 	 * atomic_cas_32() works best for a known 32-bit quantity.
79f4b3ec61Sdh155122 	 */
80f4b3ec61Sdh155122 	uint32_t		keystack_flushdump;
81f4b3ec61Sdh155122 	int			keystack_flushdump_errno;
82f4b3ec61Sdh155122 
83f4b3ec61Sdh155122 	/*
84f4b3ec61Sdh155122 	 * This integer counts the number of extended REGISTERed sockets.  This
85f4b3ec61Sdh155122 	 * determines if we should send extended REGISTERs.
86f4b3ec61Sdh155122 	 */
87f4b3ec61Sdh155122 	uint32_t		keystack_num_extended;
88f4b3ec61Sdh155122 
89f4b3ec61Sdh155122 	/*
90f4b3ec61Sdh155122 	 * Global sequence space for SADB_ACQUIRE messages of any sort.
91f4b3ec61Sdh155122 	 */
92f4b3ec61Sdh155122 	uint32_t		keystack_acquire_seq;
93f4b3ec61Sdh155122 };
94f4b3ec61Sdh155122 typedef struct keysock_stack keysock_stack_t;
95f4b3ec61Sdh155122 
96f4b3ec61Sdh155122 /*
977c478bd9Sstevel@tonic-gate  * keysock session state (one per open PF_KEY socket (i.e. as a driver))
987c478bd9Sstevel@tonic-gate  *
997c478bd9Sstevel@tonic-gate  * I keep these in a linked list, and assign a monotonically increasing
1007c478bd9Sstevel@tonic-gate  * serial ## (which is also the minor number).
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate typedef struct keysock_s {
1047c478bd9Sstevel@tonic-gate 	/* Protected by keysock_list_lock. */
1057c478bd9Sstevel@tonic-gate 	struct keysock_s *keysock_next; /* Next in list */
1067c478bd9Sstevel@tonic-gate 	struct keysock_s **keysock_ptpn; /* Pointer to previous next */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	kmutex_t keysock_lock; /* Protects the following. */
1097c478bd9Sstevel@tonic-gate 	queue_t *keysock_rq;   /* Read queue - putnext() to userland */
1107c478bd9Sstevel@tonic-gate 	queue_t *keysock_wq;   /* Write queue */
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	uint_t keysock_state;
1137c478bd9Sstevel@tonic-gate 	uint_t keysock_flags;
1147c478bd9Sstevel@tonic-gate 	/* If SADB_SATYPE_MAX (in net/pfkeyv2.h) > 255, rewhack this. */
1157c478bd9Sstevel@tonic-gate 	uint64_t keysock_registered[4]; /* Registered types for this socket. */
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	/* Also protected by keysock_list_lock. */
1187c478bd9Sstevel@tonic-gate 	minor_t keysock_serial; /* Serial number of this socket. */
119f4b3ec61Sdh155122 	keysock_stack_t		*keysock_keystack;
1207c478bd9Sstevel@tonic-gate } keysock_t;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #define	KEYSOCK_NOLOOP	0x1	/* Don't loopback messages (no replies). */
1237c478bd9Sstevel@tonic-gate #define	KEYSOCK_PROMISC	0x2	/* Give me all outbound messages. */
1247c478bd9Sstevel@tonic-gate 				/* DANGER:	Setting this requires EXTRA */
1257c478bd9Sstevel@tonic-gate 				/*		privilege on an MLS box. */
1267c478bd9Sstevel@tonic-gate #define	KEYSOCK_EXTENDED 0x4	/* Extended REGISTER received. */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* My apologies for the ugliness of this macro.  And using constants. */
1297c478bd9Sstevel@tonic-gate #define	KEYSOCK_ISREG(ks, satype) (((ks)->keysock_registered[(satype) >> 3]) & \
1307c478bd9Sstevel@tonic-gate 	(1 << ((satype) & 63)))
1317c478bd9Sstevel@tonic-gate #define	KEYSOCK_SETREG(ks, satype) (ks)->keysock_registered[(satype) >> 3] |= \
1327c478bd9Sstevel@tonic-gate 	(1 << ((satype) & 63))
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Keysock consumers (i.e. AH, ESP), in array based on sadb_msg_satype.
1367c478bd9Sstevel@tonic-gate  * For module instances.
1377c478bd9Sstevel@tonic-gate  */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate typedef struct keysock_consumer_s {
1407c478bd9Sstevel@tonic-gate 	kmutex_t kc_lock;	/* Protects instance. */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	queue_t *kc_rq;		/* Read queue, requests from AH, ESP. */
1437c478bd9Sstevel@tonic-gate 	queue_t *kc_wq;		/* Write queue, putnext down */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/* Other goodies as a need them. */
1467c478bd9Sstevel@tonic-gate 	uint8_t			kc_sa_type;	/* What sort of SA am I? */
1477c478bd9Sstevel@tonic-gate 	uint_t			kc_flags;
148f4b3ec61Sdh155122 	keysock_stack_t		*kc_keystack;
1497c478bd9Sstevel@tonic-gate } keysock_consumer_t;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /* Can only set flags when keysock_consumer_lock is held. */
1527c478bd9Sstevel@tonic-gate #define	KC_INTERNAL 0x1		/* Consumer maintained by keysock itself. */
1537c478bd9Sstevel@tonic-gate #define	KC_FLUSHING 0x2		/* SADB_FLUSH pending on this consumer. */
1547c478bd9Sstevel@tonic-gate 
155f4b3ec61Sdh155122 extern int keysock_plumb_ipsec(netstack_t *);
156*1edba515SAndy Fiddaman extern int keysock_opt_get(queue_t *, int, int, uchar_t *);
157*1edba515SAndy Fiddaman extern int keysock_opt_set(queue_t *, uint_t, int, int, uint_t,
158*1edba515SAndy Fiddaman     uchar_t *, uint_t *, uchar_t *, void *, cred_t *cr);
159*1edba515SAndy Fiddaman extern void keysock_error(keysock_t *, mblk_t *, int, int);
160*1edba515SAndy Fiddaman extern void keysock_passup(mblk_t *, sadb_msg_t *, minor_t,
161*1edba515SAndy Fiddaman     keysock_consumer_t *, boolean_t, keysock_stack_t *);
162f4b3ec61Sdh155122 
1637c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate #endif
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate #endif /* _INET_KEYSOCK_H */
168