xref: /illumos-gate/usr/src/uts/common/sys/netstack.h (revision 24da5b34)
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 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/kstat.h>
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 /*
38  * This allows various pieces in and around IP to have a separate instance
39  * for each instance of IP. This is used to support zones that have an
40  * exclusive stack.
41  * Pieces of software far removed from IP (e.g., kernel software
42  * sitting on top of TCP or UDP) probably should not use the netstack
43  * support; if such software wants to support separate zones it
44  * can do that using the zones framework (zone_key_create() etc)
45  * whether there is a shared IP stack or and exclusive IP stack underneath.
46  */
47 
48 /*
49  * Each netstack has an identifier. We reuse the zoneid allocation for
50  * this but have a separate typedef. Thus the shared stack (used by
51  * the global zone and other shared stack zones) have a zero ID, and
52  * the exclusive stacks have a netstackid that is the same as their zoneid.
53  */
54 typedef id_t	netstackid_t;
55 
56 #define	GLOBAL_NETSTACKID	0
57 
58 /*
59  * One for each module which uses netstack support.
60  * Used in netstack_register().
61  *
62  * The order of these is important for some modules both for
63  * the creation (which done in ascending order) and destruction (which is
64  * done ine in decending order).
65  */
66 #define	NS_HOOK		0
67 #define	NS_NETI		1
68 #define	NS_ARP		2
69 #define	NS_IP		3
70 #define	NS_ICMP		4
71 #define	NS_UDP		5
72 #define	NS_TCP		6
73 #define	NS_SCTP		7
74 #define	NS_RTS		8
75 #define	NS_IPSEC	9
76 #define	NS_KEYSOCK	10
77 #define	NS_SPDSOCK	11
78 #define	NS_IPSECAH	12
79 #define	NS_IPSECESP	13
80 #define	NS_TUN		14
81 #define	NS_IPF		15
82 #define	NS_STR		16	/* autopush list etc */
83 #define	NS_MAX		(NS_STR+1)
84 
85 /*
86  * One for every netstack in the system.
87  * We use a union so that the compilar and lint can provide type checking -
88  * in principle we could have
89  * #define	netstack_arp		netstack_modules[NS_ARP]
90  * etc, but that would imply void * types hence no type checking by the
91  * compiler.
92  *
93  * All the fields in netstack_t except netstack_next are protected by
94  * netstack_lock. netstack_next is protected by netstack_g_lock.
95  */
96 struct netstack {
97 	union {
98 		void	*nu_modules[NS_MAX];
99 		struct {
100 			struct hook_stack	*nu_hook;
101 			struct neti_stack	*nu_neti;
102 			struct arp_stack	*nu_arp;
103 			struct ip_stack		*nu_ip;
104 			struct icmp_stack	*nu_icmp;
105 			struct udp_stack	*nu_udp;
106 			struct tcp_stack	*nu_tcp;
107 			struct sctp_stack	*nu_sctp;
108 			struct rts_stack	*nu_rts;
109 			struct ipsec_stack	*nu_ipsec;
110 			struct keysock_stack	*nu_keysock;
111 			struct spd_stack	*nu_spdsock;
112 			struct ipsecah_stack	*nu_ipsecah;
113 			struct ipsecesp_stack	*nu_ipsecesp;
114 			struct tun_stack	*nu_tun;
115 			struct ipf_stack	*nu_ipf;
116 			struct str_stack	*nu_str;
117 		} nu_s;
118 	} netstack_u;
119 #define	netstack_modules	netstack_u.nu_modules
120 #define	netstack_hook		netstack_u.nu_s.nu_hook
121 #define	netstack_neti		netstack_u.nu_s.nu_neti
122 #define	netstack_arp		netstack_u.nu_s.nu_arp
123 #define	netstack_ip		netstack_u.nu_s.nu_ip
124 #define	netstack_icmp		netstack_u.nu_s.nu_icmp
125 #define	netstack_udp		netstack_u.nu_s.nu_udp
126 #define	netstack_tcp		netstack_u.nu_s.nu_tcp
127 #define	netstack_sctp		netstack_u.nu_s.nu_sctp
128 #define	netstack_rts		netstack_u.nu_s.nu_rts
129 #define	netstack_ipsec		netstack_u.nu_s.nu_ipsec
130 #define	netstack_keysock	netstack_u.nu_s.nu_keysock
131 #define	netstack_spdsock	netstack_u.nu_s.nu_spdsock
132 #define	netstack_ipsecah	netstack_u.nu_s.nu_ipsecah
133 #define	netstack_ipsecesp	netstack_u.nu_s.nu_ipsecesp
134 #define	netstack_tun		netstack_u.nu_s.nu_tun
135 #define	netstack_ipf		netstack_u.nu_s.nu_ipf
136 #define	netstack_str		netstack_u.nu_s.nu_str
137 
138 	uint16_t	netstack_m_state[NS_MAX]; /* module state */
139 
140 	kmutex_t	netstack_lock;
141 	struct netstack *netstack_next;
142 	netstackid_t	netstack_stackid;
143 	int		netstack_numzones;	/* Number of zones using this */
144 	int		netstack_refcnt;	/* Number of hold-rele */
145 	int		netstack_flags;	/* See below */
146 };
147 typedef struct netstack netstack_t;
148 
149 /* netstack_flags values */
150 #define	NSF_UNINIT	0x01		/* Not initialized */
151 #define	NSF_CLOSING	0x02		/* Going away */
152 
153 /*
154  * State for each module for each stack - netstack_m_state[moduleid]
155  * Keeps track of pending actions to avoid holding looks when
156  * calling into the create/shutdown/destroy functions in the module.
157  */
158 #define	NSS_CREATE_NEEDED	0x0001
159 #define	NSS_CREATE_INPROGRESS	0x0002
160 #define	NSS_CREATE_COMPLETED	0x0004
161 #define	NSS_SHUTDOWN_NEEDED	0x0010
162 #define	NSS_SHUTDOWN_INPROGRESS	0x0020
163 #define	NSS_SHUTDOWN_COMPLETED	0x0040
164 #define	NSS_DESTROY_NEEDED	0x0100
165 #define	NSS_DESTROY_INPROGRESS	0x0200
166 #define	NSS_DESTROY_COMPLETED	0x0400
167 
168 #define	NSS_CREATE_ALL	\
169 	(NSS_CREATE_NEEDED|NSS_CREATE_INPROGRESS|NSS_CREATE_COMPLETED)
170 #define	NSS_SHUTDOWN_ALL	\
171 	(NSS_SHUTDOWN_NEEDED|NSS_SHUTDOWN_INPROGRESS|NSS_SHUTDOWN_COMPLETED)
172 #define	NSS_DESTROY_ALL	\
173 	(NSS_DESTROY_NEEDED|NSS_DESTROY_INPROGRESS|NSS_DESTROY_COMPLETED)
174 
175 /*
176  * One for each of the NS_* values.
177  */
178 struct netstack_registry {
179 	int		nr_flags;	/* 0 if nothing registered */
180 	void		*(*nr_create)(netstackid_t, netstack_t *);
181 	void		(*nr_shutdown)(netstackid_t, void *);
182 	void		(*nr_destroy)(netstackid_t, void *);
183 };
184 
185 /* nr_flags values */
186 #define	NRF_REGISTERED	0x01
187 
188 /*
189  * To support kstat_create_netstack() using kstat_add_zone we need
190  * to track both
191  *  - all zoneids that use the global/shared stack
192  *  - all kstats that have been added for the shared stack
193  */
194 
195 extern void netstack_init(void);
196 extern void netstack_hold(netstack_t *);
197 extern void netstack_rele(netstack_t *);
198 extern netstack_t *netstack_find_by_cred(const cred_t *);
199 extern netstack_t *netstack_find_by_stackid(netstackid_t);
200 extern netstack_t *netstack_find_by_zoneid(zoneid_t);
201 
202 extern zoneid_t netstackid_to_zoneid(netstackid_t);
203 extern netstackid_t zoneid_to_netstackid(zoneid_t);
204 
205 extern netstack_t *netstack_get_current(void);
206 
207 /*
208  * Register interest in changes to the set of netstacks.
209  * The createfn and destroyfn are required, but the shutdownfn can be
210  * NULL.
211  * Note that due to the current zsd implementation, when the create
212  * function is called the zone isn't fully present, thus functions
213  * like zone_find_by_* will fail, hence the create function can not
214  * use many zones kernel functions including zcmn_err().
215  */
216 extern void	netstack_register(int,
217     void *(*)(netstackid_t, netstack_t *),
218     void (*)(netstackid_t, void *),
219     void (*)(netstackid_t, void *));
220 extern void	netstack_unregister(int);
221 extern kstat_t	*kstat_create_netstack(char *, int, char *, char *, uchar_t,
222     uint_t, uchar_t, netstackid_t);
223 extern void	kstat_delete_netstack(kstat_t *, netstackid_t);
224 
225 /*
226  * Simple support for walking all the netstacks.
227  * The caller of netstack_next() needs to call netstack_rele() when
228  * done with a netstack.
229  */
230 typedef	int	netstack_handle_t;
231 
232 extern void	netstack_next_init(netstack_handle_t *);
233 extern void	netstack_next_fini(netstack_handle_t *);
234 extern netstack_t	*netstack_next(netstack_handle_t *);
235 
236 #ifdef	__cplusplus
237 }
238 #endif
239 
240 
241 #endif	/* _SYS_NETSTACK_H */
242