xref: /titanic_41/usr/src/uts/common/sys/netstack.h (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 #ifndef _SYS_NETSTACK_H
27 #define	_SYS_NETSTACK_H
28 
29 #include <sys/kstat.h>
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 /*
36  * This allows various pieces in and around IP to have a separate instance
37  * for each instance of IP. This is used to support zones that have an
38  * exclusive stack.
39  * Pieces of software far removed from IP (e.g., kernel software
40  * sitting on top of TCP or UDP) probably should not use the netstack
41  * support; if such software wants to support separate zones it
42  * can do that using the zones framework (zone_key_create() etc)
43  * whether there is a shared IP stack or and exclusive IP stack underneath.
44  */
45 
46 /*
47  * Each netstack has an identifier. We reuse the zoneid allocation for
48  * this but have a separate typedef. Thus the shared stack (used by
49  * the global zone and other shared stack zones) have a zero ID, and
50  * the exclusive stacks have a netstackid that is the same as their zoneid.
51  */
52 typedef id_t	netstackid_t;
53 
54 #define	GLOBAL_NETSTACKID	0
55 
56 /*
57  * One for each module which uses netstack support.
58  * Used in netstack_register().
59  *
60  * The order of these is important for some modules both for
61  * the creation (which done in ascending order) and destruction (which is
62  * done ine in decending order).
63  */
64 #define	NS_ALL		-1	/* Match all */
65 #define	NS_STR		0	/* autopush list etc */
66 #define	NS_HOOK		1
67 #define	NS_NETI		2
68 #define	NS_ARP		3
69 #define	NS_IP		4
70 #define	NS_ICMP		5
71 #define	NS_UDP		6
72 #define	NS_TCP		7
73 #define	NS_SCTP		8
74 #define	NS_RTS		9
75 #define	NS_IPSEC	10
76 #define	NS_KEYSOCK	11
77 #define	NS_SPDSOCK	12
78 #define	NS_IPSECAH	13
79 #define	NS_IPSECESP	14
80 #define	NS_TUN		15
81 #define	NS_IPNET	16
82 #define	NS_MAX		(NS_IPNET+1)
83 
84 /*
85  * State maintained for each module which tracks the state of
86  * the create, shutdown and destroy callbacks.
87  *
88  * Keeps track of pending actions to avoid holding locks when
89  * calling into the create/shutdown/destroy functions in the module.
90  */
91 #ifdef _KERNEL
92 typedef struct {
93 	uint16_t 	nms_flags;
94 	kcondvar_t	nms_cv;
95 } nm_state_t;
96 
97 /*
98  * nms_flags
99  */
100 #define	NSS_CREATE_NEEDED	0x0001
101 #define	NSS_CREATE_INPROGRESS	0x0002
102 #define	NSS_CREATE_COMPLETED	0x0004
103 #define	NSS_SHUTDOWN_NEEDED	0x0010
104 #define	NSS_SHUTDOWN_INPROGRESS	0x0020
105 #define	NSS_SHUTDOWN_COMPLETED	0x0040
106 #define	NSS_DESTROY_NEEDED	0x0100
107 #define	NSS_DESTROY_INPROGRESS	0x0200
108 #define	NSS_DESTROY_COMPLETED	0x0400
109 
110 #define	NSS_CREATE_ALL	\
111 	(NSS_CREATE_NEEDED|NSS_CREATE_INPROGRESS|NSS_CREATE_COMPLETED)
112 #define	NSS_SHUTDOWN_ALL	\
113 	(NSS_SHUTDOWN_NEEDED|NSS_SHUTDOWN_INPROGRESS|NSS_SHUTDOWN_COMPLETED)
114 #define	NSS_DESTROY_ALL	\
115 	(NSS_DESTROY_NEEDED|NSS_DESTROY_INPROGRESS|NSS_DESTROY_COMPLETED)
116 
117 #define	NSS_ALL_INPROGRESS	\
118 	(NSS_CREATE_INPROGRESS|NSS_SHUTDOWN_INPROGRESS|NSS_DESTROY_INPROGRESS)
119 #else
120 /* User-level compile like IP Filter needs a netstack_t. Dummy */
121 typedef uint_t nm_state_t;
122 #endif /* _KERNEL */
123 
124 /*
125  * One for every netstack in the system.
126  * We use a union so that the compilar and lint can provide type checking -
127  * in principle we could have
128  * #define	netstack_arp		netstack_modules[NS_ARP]
129  * etc, but that would imply void * types hence no type checking by the
130  * compiler.
131  *
132  * All the fields in netstack_t except netstack_next are protected by
133  * netstack_lock. netstack_next is protected by netstack_g_lock.
134  */
135 struct netstack {
136 	union {
137 		void	*nu_modules[NS_MAX];
138 		struct {
139 			struct str_stack	*nu_str;
140 			struct hook_stack	*nu_hook;
141 			struct neti_stack	*nu_neti;
142 			struct arp_stack	*nu_arp;
143 			struct ip_stack		*nu_ip;
144 			struct icmp_stack	*nu_icmp;
145 			struct udp_stack	*nu_udp;
146 			struct tcp_stack	*nu_tcp;
147 			struct sctp_stack	*nu_sctp;
148 			struct rts_stack	*nu_rts;
149 			struct ipsec_stack	*nu_ipsec;
150 			struct keysock_stack	*nu_keysock;
151 			struct spd_stack	*nu_spdsock;
152 			struct ipsecah_stack	*nu_ipsecah;
153 			struct ipsecesp_stack	*nu_ipsecesp;
154 			struct tun_stack	*nu_tun;
155 			struct ipnet_stack	*nu_ipnet;
156 		} nu_s;
157 	} netstack_u;
158 #define	netstack_modules	netstack_u.nu_modules
159 #define	netstack_str		netstack_u.nu_s.nu_str
160 #define	netstack_hook		netstack_u.nu_s.nu_hook
161 #define	netstack_neti		netstack_u.nu_s.nu_neti
162 #define	netstack_arp		netstack_u.nu_s.nu_arp
163 #define	netstack_ip		netstack_u.nu_s.nu_ip
164 #define	netstack_icmp		netstack_u.nu_s.nu_icmp
165 #define	netstack_udp		netstack_u.nu_s.nu_udp
166 #define	netstack_tcp		netstack_u.nu_s.nu_tcp
167 #define	netstack_sctp		netstack_u.nu_s.nu_sctp
168 #define	netstack_rts		netstack_u.nu_s.nu_rts
169 #define	netstack_ipsec		netstack_u.nu_s.nu_ipsec
170 #define	netstack_keysock	netstack_u.nu_s.nu_keysock
171 #define	netstack_spdsock	netstack_u.nu_s.nu_spdsock
172 #define	netstack_ipsecah	netstack_u.nu_s.nu_ipsecah
173 #define	netstack_ipsecesp	netstack_u.nu_s.nu_ipsecesp
174 #define	netstack_tun		netstack_u.nu_s.nu_tun
175 #define	netstack_ipnet		netstack_u.nu_s.nu_ipnet
176 
177 	nm_state_t	netstack_m_state[NS_MAX]; /* module state */
178 
179 	kmutex_t	netstack_lock;
180 	struct netstack *netstack_next;
181 	netstackid_t	netstack_stackid;
182 	int		netstack_numzones;	/* Number of zones using this */
183 	int		netstack_refcnt;	/* Number of hold-rele */
184 	int		netstack_flags;	/* See below */
185 
186 #ifdef _KERNEL
187 	/* Needed to ensure that we run the callback functions in order */
188 	kcondvar_t	netstack_cv;
189 #endif
190 };
191 typedef struct netstack netstack_t;
192 
193 /* netstack_flags values */
194 #define	NSF_UNINIT		0x01		/* Not initialized */
195 #define	NSF_CLOSING		0x02		/* Going away */
196 #define	NSF_ZONE_CREATE		0x04		/* create callbacks inprog */
197 #define	NSF_ZONE_SHUTDOWN	0x08		/* shutdown callbacks */
198 #define	NSF_ZONE_DESTROY	0x10		/* destroy callbacks */
199 
200 #define	NSF_ZONE_INPROGRESS	\
201 	(NSF_ZONE_CREATE|NSF_ZONE_SHUTDOWN|NSF_ZONE_DESTROY)
202 
203 /*
204  * One for each of the NS_* values.
205  */
206 struct netstack_registry {
207 	int		nr_flags;	/* 0 if nothing registered */
208 	void		*(*nr_create)(netstackid_t, netstack_t *);
209 	void		(*nr_shutdown)(netstackid_t, void *);
210 	void		(*nr_destroy)(netstackid_t, void *);
211 };
212 
213 /* nr_flags values */
214 #define	NRF_REGISTERED	0x01
215 #define	NRF_DYING	0x02	/* No new creates */
216 
217 /*
218  * To support kstat_create_netstack() using kstat_add_zone we need
219  * to track both
220  *  - all zoneids that use the global/shared stack
221  *  - all kstats that have been added for the shared stack
222  */
223 
224 extern void netstack_init(void);
225 extern void netstack_hold(netstack_t *);
226 extern void netstack_rele(netstack_t *);
227 extern netstack_t *netstack_find_by_cred(const cred_t *);
228 extern netstack_t *netstack_find_by_stackid(netstackid_t);
229 extern netstack_t *netstack_find_by_zoneid(zoneid_t);
230 
231 extern zoneid_t netstackid_to_zoneid(netstackid_t);
232 extern netstackid_t zoneid_to_netstackid(zoneid_t);
233 
234 extern netstack_t *netstack_get_current(void);
235 
236 /*
237  * Register interest in changes to the set of netstacks.
238  * The createfn and destroyfn are required, but the shutdownfn can be
239  * NULL.
240  * Note that due to the current zsd implementation, when the create
241  * function is called the zone isn't fully present, thus functions
242  * like zone_find_by_* will fail, hence the create function can not
243  * use many zones kernel functions including zcmn_err().
244  */
245 extern void	netstack_register(int,
246     void *(*)(netstackid_t, netstack_t *),
247     void (*)(netstackid_t, void *),
248     void (*)(netstackid_t, void *));
249 extern void	netstack_unregister(int);
250 extern kstat_t	*kstat_create_netstack(char *, int, char *, char *, uchar_t,
251     uint_t, uchar_t, netstackid_t);
252 extern void	kstat_delete_netstack(kstat_t *, netstackid_t);
253 
254 /*
255  * Simple support for walking all the netstacks.
256  * The caller of netstack_next() needs to call netstack_rele() when
257  * done with a netstack.
258  */
259 typedef	int	netstack_handle_t;
260 
261 extern void	netstack_next_init(netstack_handle_t *);
262 extern void	netstack_next_fini(netstack_handle_t *);
263 extern netstack_t	*netstack_next(netstack_handle_t *);
264 
265 #ifdef	__cplusplus
266 }
267 #endif
268 
269 
270 #endif	/* _SYS_NETSTACK_H */
271