xref: /illumos-gate/usr/src/uts/common/sys/neti.h (revision 8c69cc8fbe729fa7b091e901c4b50508ccc6bb33)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_NETI_H
27 #define	_SYS_NETI_H
28 
29 #include <netinet/in.h>
30 #include <sys/int_types.h>
31 #include <sys/queue.h>
32 #include <sys/hook_impl.h>
33 #include <sys/netstack.h>
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 struct msgb;	/* avoiding sys/stream.h here */
40 
41 #define	NETINFO_VERSION 1
42 
43 /*
44  * Network hooks framework stack protocol name
45  */
46 #define	NHF_INET	"NHF_INET"
47 #define	NHF_INET6	"NHF_INET6"
48 #define	NHF_ARP		"NHF_ARP"
49 
50 /*
51  * Event identification
52  */
53 #define	NH_PHYSICAL_IN	"PHYSICAL_IN"
54 #define	NH_PHYSICAL_OUT	"PHYSICAL_OUT"
55 #define	NH_FORWARDING	"FORWARDING"
56 #define	NH_LOOPBACK_IN	"LOOPBACK_IN"
57 #define	NH_LOOPBACK_OUT	"LOOPBACK_OUT"
58 #define	NH_NIC_EVENTS	"NIC_EVENTS"
59 #define	NH_OBSERVE	"OBSERVING"
60 
61 /*
62  * Network NIC hardware checksum capability
63  */
64 #define	NET_HCK_NONE   	0x00
65 #define	NET_HCK_L3_FULL	0x01
66 #define	NET_HCK_L3_PART	0x02
67 #define	NET_HCK_L4_FULL	0x10
68 #define	NET_HCK_L4_PART	0x20
69 
70 #define	NET_IS_HCK_L3_FULL(n, x)                                             \
71 	((net_ispartialchecksum(n, x) & NET_HCK_L3_FULL) == NET_HCK_L3_FULL)
72 #define	NET_IS_HCK_L3_PART(n, x)                                             \
73 	((net_ispartialchecksum(n, x) & NET_HCK_L3_PART) == NET_HCK_L3_PART)
74 #define	NET_IS_HCK_L4_FULL(n, x)                                             \
75 	((net_ispartialchecksum(n, x) & NET_HCK_L4_FULL) == NET_HCK_L4_FULL)
76 #define	NET_IS_HCK_L4_PART(n, x)                                             \
77 	((net_ispartialchecksum(n, x) & NET_HCK_L4_PART) == NET_HCK_L4_PART)
78 #define	NET_IS_HCK_L34_FULL(n, x)                                            \
79 	((net_ispartialchecksum(n, x) & (NET_HCK_L3_FULL|NET_HCK_L4_FULL))   \
80 	    == (NET_HCK_L3_FULL | NET_HCK_L4_FULL))
81 
82 typedef uintptr_t	phy_if_t;
83 typedef intptr_t	lif_if_t;
84 typedef uintptr_t	net_ifdata_t;
85 typedef id_t		netid_t;
86 
87 /*
88  * Netinfo interface specification
89  *
90  * Netinfo provides an extensible and easy to use interface for
91  * accessing data and functionality already embedded within network
92  * code that exists within the kernel.
93  */
94 typedef enum net_ifaddr {
95 	NA_ADDRESS = 1,
96 	NA_PEER,
97 	NA_BROADCAST,
98 	NA_NETMASK
99 } net_ifaddr_t;
100 
101 
102 typedef enum inject {
103 	NI_QUEUE_IN = 1,
104 	NI_QUEUE_OUT,
105 	NI_DIRECT_OUT
106 } inject_t;
107 
108 /*
109  * net_inject - public interface
110  */
111 typedef struct net_inject {
112 	int			ni_version;
113 	netid_t			ni_netid;
114 	struct msgb		*ni_packet;
115 	struct sockaddr_storage	ni_addr;
116 	phy_if_t		ni_physical;
117 } net_inject_t;
118 
119 typedef struct net_data *net_handle_t;
120 
121 /*
122  * net_protocol_t private interface
123  */
124 struct net_protocol_s {
125 	int		netp_version;
126 	char		*netp_name;
127 	int		(*netp_getifname)(net_handle_t, phy_if_t, char *,
128 			    const size_t);
129 	int		(*netp_getmtu)(net_handle_t, phy_if_t, lif_if_t);
130 	int		(*netp_getpmtuenabled)(net_handle_t);
131 	int		(*netp_getlifaddr)(net_handle_t, phy_if_t, lif_if_t,
132 			    size_t, net_ifaddr_t [], void *);
133 	int		(*neti_getlifzone)(net_handle_t, phy_if_t, lif_if_t,
134 			    zoneid_t *);
135 	int		(*neti_getlifflags)(net_handle_t, phy_if_t, lif_if_t,
136 			    uint64_t *);
137 	phy_if_t	(*netp_phygetnext)(net_handle_t, phy_if_t);
138 	phy_if_t	(*netp_phylookup)(net_handle_t, const char *);
139 	lif_if_t	(*netp_lifgetnext)(net_handle_t, phy_if_t, lif_if_t);
140 	int		(*netp_inject)(net_handle_t, inject_t, net_inject_t *);
141 	phy_if_t	(*netp_routeto)(net_handle_t, struct sockaddr *,
142 			    struct sockaddr *);
143 	int		(*netp_ispartialchecksum)(net_handle_t, struct msgb *);
144 	int		(*netp_isvalidchecksum)(net_handle_t, struct msgb *);
145 };
146 typedef struct net_protocol_s net_protocol_t;
147 
148 
149 /*
150  * Private data structures
151  */
152 struct net_data {
153 	LIST_ENTRY(net_data)		netd_list;
154 	net_protocol_t			netd_info;
155 	int				netd_refcnt;
156 	hook_family_int_t		*netd_hooks;
157 	struct neti_stack_s		*netd_stack;
158 	int				netd_condemned;
159 };
160 
161 
162 typedef struct injection_s {
163 	net_inject_t	inj_data;
164 	boolean_t	inj_isv6;
165 	void *		inj_ptr;
166 } injection_t;
167 
168 /*
169  * The ipif_id space is [0,MAX) but this interface wants to return [1,MAX] as
170  * a valid range of logical interface numbers so that it can return 0 to mean
171  * "end of list" with net_lifgetnext.  Changing ipif_id's to use the [1,MAX]
172  * space is something to be considered for the future, if it is worthwhile.
173  */
174 #define	MAP_IPIF_ID(x)		((x) + 1)
175 #define	UNMAP_IPIF_ID(x)	(((x) > 0) ? (x) - 1 : (x))
176 
177 struct net_instance_s {
178 	int				nin_version;
179 	char				*nin_name;
180 	void				*(*nin_create)(const netid_t);
181 	void				(*nin_destroy)(const netid_t, void *);
182 	void				(*nin_shutdown)(const netid_t, void *);
183 };
184 typedef struct net_instance_s net_instance_t;
185 
186 struct net_instance_int_s {
187 	LIST_ENTRY(net_instance_int_s)	nini_next;
188 	uint_t				nini_ref;
189 	void				*nini_created;
190 	struct net_instance_int_s	*nini_parent;
191 	net_instance_t			*nini_instance;
192 	hook_notify_t			nini_notify;
193 	uint32_t			nini_flags;
194 	kcondvar_t			nini_cv;
195 	boolean_t			nini_condemned;
196 };
197 typedef struct net_instance_int_s net_instance_int_t;
198 LIST_HEAD(nini_head_s, net_instance_int_s);
199 typedef struct nini_head_s nini_head_t;
200 
201 #define	nini_version	nini_instance->nin_version
202 #define	nini_name	nini_instance->nin_name
203 #define	nini_create	nini_instance->nin_create
204 #define	nini_destroy	nini_instance->nin_destroy
205 #define	nini_shutdown	nini_instance->nin_shutdown
206 
207 /*
208  * netinfo stack instances
209  */
210 struct neti_stack_s {
211 	kmutex_t			nts_lock;
212 	LIST_ENTRY(neti_stack_s)	nts_next;
213 	netid_t				nts_id;
214 	zoneid_t			nts_zoneid;
215 	netstackid_t			nts_stackid;
216 	netstack_t			*nts_netstack;
217 	nini_head_t			nts_instances;
218 	uint32_t			nts_flags;
219 	kcondvar_t			nts_cv;
220 	/* list of net_handle_t */
221 	LIST_HEAD(netd_listhead, net_data) nts_netd_head;
222 };
223 typedef struct neti_stack_s neti_stack_t;
224 LIST_HEAD(neti_stack_head_s, neti_stack_s);
225 typedef struct neti_stack_head_s neti_stack_head_t;
226 
227 /*
228  * Internal functions that need to be exported within the module.
229  */
230 extern void neti_init(void);
231 extern void neti_fini(void);
232 extern neti_stack_t *net_getnetistackbyid(netid_t);
233 extern netstackid_t net_getnetstackidbynetid(netid_t);
234 extern netid_t net_getnetidbynetstackid(netstackid_t);
235 extern netid_t net_zoneidtonetid(zoneid_t);
236 extern zoneid_t net_getzoneidbynetid(netid_t);
237 
238 /*
239  * Functions available for public use.
240  */
241 extern hook_event_token_t net_event_register(net_handle_t, hook_event_t *);
242 extern int net_event_shutdown(net_handle_t, hook_event_t *);
243 extern int net_event_unregister(net_handle_t, hook_event_t *);
244 extern int net_event_notify_register(net_handle_t, char *,
245     hook_notify_fn_t, void *);
246 extern int net_event_notify_unregister(net_handle_t, char *, hook_notify_fn_t);
247 
248 extern int net_family_register(net_handle_t, hook_family_t *);
249 extern int net_family_shutdown(net_handle_t, hook_family_t *);
250 extern int net_family_unregister(net_handle_t, hook_family_t *);
251 
252 extern int net_hook_register(net_handle_t, char *, hook_t *);
253 extern int net_hook_unregister(net_handle_t, char *, hook_t *);
254 
255 extern int net_inject(net_handle_t, inject_t, net_inject_t *);
256 extern net_inject_t *net_inject_alloc(const int);
257 extern void net_inject_free(net_inject_t *);
258 
259 extern net_instance_t *net_instance_alloc(const int version);
260 extern void net_instance_free(net_instance_t *);
261 extern int net_instance_register(net_instance_t *);
262 extern int net_instance_unregister(net_instance_t *);
263 extern int net_instance_notify_register(netid_t, hook_notify_fn_t, void *);
264 extern int net_instance_notify_unregister(netid_t netid, hook_notify_fn_t);
265 
266 extern kstat_t *net_kstat_create(netid_t, char *, int, char *, char *,
267     uchar_t, ulong_t, uchar_t);
268 extern void net_kstat_delete(netid_t, kstat_t *);
269 
270 extern net_handle_t net_protocol_lookup(netid_t, const char *);
271 extern net_handle_t net_protocol_register(netid_t, const net_protocol_t *);
272 extern int net_protocol_release(net_handle_t);
273 extern int net_protocol_unregister(net_handle_t);
274 extern net_handle_t net_protocol_walk(netid_t, net_handle_t);
275 extern int net_protocol_notify_register(net_handle_t, hook_notify_fn_t, void *);
276 extern int net_protocol_notify_unregister(net_handle_t, hook_notify_fn_t);
277 
278 
279 extern int net_getifname(net_handle_t, phy_if_t, char *, const size_t);
280 extern int net_getmtu(net_handle_t, phy_if_t, lif_if_t);
281 extern int net_getpmtuenabled(net_handle_t);
282 extern int net_getlifaddr(net_handle_t, phy_if_t, lif_if_t,
283     int, net_ifaddr_t [], void *);
284 extern zoneid_t net_getlifzone(net_handle_t, phy_if_t, lif_if_t, zoneid_t *);
285 extern int net_getlifflags(net_handle_t, phy_if_t, lif_if_t, uint64_t *);
286 extern phy_if_t net_phygetnext(net_handle_t, phy_if_t);
287 extern phy_if_t net_phylookup(net_handle_t, const char *);
288 extern lif_if_t net_lifgetnext(net_handle_t, phy_if_t, lif_if_t);
289 extern phy_if_t net_routeto(net_handle_t, struct sockaddr *,
290     struct sockaddr *);
291 extern int net_ispartialchecksum(net_handle_t, struct msgb *);
292 extern int net_isvalidchecksum(net_handle_t, struct msgb *);
293 
294 #ifdef	__cplusplus
295 }
296 #endif
297 
298 #endif /* _SYS_NETI_H */
299