xref: /titanic_41/usr/src/uts/common/inet/ipnet.h (revision 9e86db79b7d1bbc5f2f04e99954cbd5eae0e22bb)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _INET_IPNET_H
28 #define	_INET_IPNET_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/types.h>
35 #include <sys/netstack.h>
36 #include <sys/list.h>
37 #include <netinet/in.h>
38 #include <net/if.h>
39 #include <net/bpf.h>
40 #include <sys/avl.h>
41 #include <sys/neti.h>
42 #include <sys/hook_event.h>
43 #include <sys/zone.h>
44 #include <sys/kstat.h>
45 
46 typedef struct ipnet_kstats_s	{
47 	kstat_named_t	ik_duplicationFail;
48 	kstat_named_t	ik_dispatchOk;
49 	kstat_named_t	ik_dispatchFail;
50 	kstat_named_t	ik_dispatchHeaderDrop;
51 	kstat_named_t	ik_dispatchDupDrop;
52 	kstat_named_t	ik_dispatchPutDrop;
53 	kstat_named_t	ik_dispatchDeliver;
54 	kstat_named_t	ik_acceptOk;
55 	kstat_named_t	ik_acceptFail;
56 } ipnet_kstats_t;
57 
58 #define	IPSK_BUMP(_x, _y)	(_x)->ips_stats._y.value.ui64++
59 
60 /*
61  * Structure used to hold information for both IPv4 and IPv6 addresses.
62  *
63  * When ifa_shared is non-NULL, it points to a "fake" ipnetif_t structure
64  * that represents the network interface for each zone that shares its
65  * network stack. This is used by BPF to build a list of interface names
66  * present in each zone. Multiple ipnetif_addr_t's may point to a single
67  * ipnetif_t using ifa_shared. The typical case is the global zone has
68  * a bge0 that other zones use as bge0:1, bge0:2, etc. In ipnet, the
69  * ipnetif_addr_t's that store the IP address for bge0:1, etc, would
70  * point to an ipnetif_t stored in the if_avl_by_shared tree that has
71  * the name "bge0".
72  */
73 typedef struct ipnetif_addr {
74 	union {
75 		ipaddr_t	ifau_ip4addr;
76 		in6_addr_t	ifau_ip6addr;
77 	} ifa_addr;
78 	ipaddr_t	ifa_brdaddr;
79 	zoneid_t	ifa_zone;
80 	uint64_t	ifa_id;
81 	list_node_t	ifa_link;
82 	struct ipnetif	*ifa_shared;
83 } ipnetif_addr_t;
84 #define	ifa_ip4addr	ifa_addr.ifau_ip4addr
85 #define	ifa_ip6addr	ifa_addr.ifau_ip6addr
86 
87 /*
88  * Structure describes the ipnet module representation of an ip interface.
89  * The structure holds both IPv4 and IPv6 addresses, the address lists are
90  * protected by a mutex. The ipnetif structures are held per stack instance
91  * within avl trees indexed on name and ip index.
92  *
93  * if_avl_by_shared is used by zones that share their instance of IP with
94  * other zones. It is used to store ipnetif_t structures. An example of this
95  * is the global zone sharing its instance of IP with other local zones.
96  * In this case, if_avl_by_shared is a tree of names that are in active use
97  * by zones using a shared instance of IP.
98  * The value in if_sharecnt represents the number of ipnetif_addr_t's that
99  * point to it.
100  */
101 typedef struct ipnetif {
102 	char		if_name[LIFNAMSIZ];
103 	uint_t		if_flags;
104 	uint_t		if_index;
105 	kmutex_t	if_addr_lock;	/* protects both addr lists */
106 	list_t		if_ip4addr_list;
107 	list_t		if_ip6addr_list;
108 	avl_node_t	if_avl_by_index;
109 	avl_node_t	if_avl_by_name;
110 	dev_t		if_dev;
111 	uint_t		if_multicnt;	/* protected by ips_event_lock */
112 	kmutex_t	if_reflock;	/* protects if_refcnt */
113 	int		if_refcnt;	/* if_reflock */
114 	zoneid_t	if_zoneid;
115 	avl_node_t	if_avl_by_shared;	/* protected by ips_avl_lock */
116 	struct ipnet_stack *if_stackp;
117 	int		if_sharecnt;	/* protected by if_reflock */
118 } ipnetif_t;
119 
120 /* if_flags */
121 #define	IPNETIF_IPV4PLUMBED	0x01
122 #define	IPNETIF_IPV6PLUMBED	0x02
123 #define	IPNETIF_IPV4ALLMULTI	0x04
124 #define	IPNETIF_IPV6ALLMULTI	0x08
125 #define	IPNETIF_LOOPBACK	0x10
126 
127 /*
128  * Structure used by the accept callback function.  This is simply an address
129  * pointer into a packet (either IPv4 or IPv6), along with an address family
130  * that denotes which pointer is valid.
131  */
132 typedef struct ipnet_addrp {
133 	sa_family_t	iap_family;
134 	union {
135 		ipaddr_t	*iapu_addr4;
136 		in6_addr_t	*iapu_addr6;
137 	} iap_addrp;
138 } ipnet_addrp_t;
139 #define	iap_addr4	iap_addrp.iapu_addr4
140 #define	iap_addr6	iap_addrp.iapu_addr6
141 
142 struct ipnet;
143 struct ipobs_hook_data;
144 typedef boolean_t ipnet_acceptfn_t(struct ipnet *, struct hook_pkt_observe_s *,
145     ipnet_addrp_t *, ipnet_addrp_t *);
146 
147 /*
148  * Per instance data for all open streams. Instance data is held on a
149  * per netstack list see struct ipnet_stack below.
150  */
151 typedef struct ipnet {
152 	queue_t		*ipnet_rq;	/* read queue pointer */
153 	minor_t		ipnet_minor;	/* minor number for this instance */
154 	ipnetif_t	*ipnet_if;	/* ipnetif for this open instance */
155 	zoneid_t	ipnet_zoneid;	/* zoneid the device was opened in */
156 	uint_t		ipnet_flags;	/* see below */
157 	t_scalar_t	ipnet_family;	/* protocol family of this instance */
158 	t_uscalar_t	ipnet_dlstate;	/* dlpi state */
159 	list_node_t	ipnet_next;	/* list next member */
160 	netstack_t	*ipnet_ns;	/* netstack of zone we were opened in */
161 	ipnet_acceptfn_t *ipnet_acceptfn; /* accept callback function pointer */
162 	hook_t		*ipnet_hook;	/* hook token to unregister */
163 	void		*ipnet_data;	/* value to pass back to bpf_itap */
164 } ipnet_t;
165 
166 /* ipnet_flags */
167 #define	IPNET_PROMISC_PHYS	0x01
168 #define	IPNET_PROMISC_MULTI	0x02
169 #define	IPNET_PROMISC_SAP	0x04
170 #define	IPNET_INFO		0x08
171 #define	IPNET_LOMODE		0x10
172 
173 /*
174  * Per-netstack data holding:
175  * - net_handle_t references for IPv4 and IPv6 for this netstack.
176  * - avl trees by name and index for ip interfaces associated with this
177  *   netstack. The trees are protected by ips_avl_lock.
178  * - ips_str_list is a list of open client streams.  ips_walkers_lock in
179  *   conjunction with ips_walkers_cv and ips_walkers_cnt synchronize access to
180  *   the list.  The count is incremented in ipnet_dispatch() at the start of a
181  *   walk and decremented when the walk is finished. If the walkers count is 0
182  *   then we cv_broadcast() waiting any threads waiting on the walkers count.
183  * - ips_event_lock synchronizes ipnet_if_init() and incoming NIC info events.
184  *   We cannot be processing any NIC info events while initializing interfaces
185  *   in ipnet_if_init().
186  *
187  * Note on lock ordering: If a thread needs to both hold the ips_event_lock
188  * and any other lock such as ips_walkers_lock, ips_avl_lock, or if_addr_lock,
189  * the ips_event_lock must be held first.  This lock ordering is mandated by
190  * ipnet_nicevent_cb() which must always grab ips_event_lock before continuing
191  * with processing NIC events.
192  */
193 typedef struct ipnet_stack {
194 	net_handle_t	ips_ndv4;
195 	net_handle_t	ips_ndv6;
196 	netstack_t	*ips_netstack;
197 	hook_t		*ips_nicevents;
198 	kmutex_t	ips_event_lock;
199 	kmutex_t	ips_avl_lock;
200 	avl_tree_t	ips_avl_by_index;
201 	avl_tree_t	ips_avl_by_name;
202 	kmutex_t	ips_walkers_lock;
203 	kcondvar_t	ips_walkers_cv;
204 	uint_t		ips_walkers_cnt;
205 	list_t		ips_str_list;
206 	kstat_t		*ips_kstatp;
207 	ipnet_kstats_t	ips_stats;
208 	bpf_attach_fn_t	ips_bpfattach_fn;
209 	bpf_detach_fn_t	ips_bpfdetach_fn;
210 	avl_tree_t	ips_avl_by_shared;
211 	hook_t		*ips_hook;
212 } ipnet_stack_t;
213 
214 /*
215  * Template for dl_info_ack_t initialization.  We don't have an address, so we
216  * set the address length to just the SAP length (16 bits).  We don't really
217  * have a maximum SDU, but setting it to UINT_MAX proved problematic with
218  * applications that performed arithmetic on dl_max_sdu and wrapped around, so
219  * we sleaze out and use INT_MAX.
220  */
221 #define	IPNET_INFO_ACK_INIT {						\
222 	DL_INFO_ACK,			/* dl_primitive */		\
223 	INT_MAX,			/* dl_max_sdu */		\
224 	0,				/* dl_min_sdu */		\
225 	sizeof (uint16_t),		/* dl_addr_length */ 		\
226 	DL_IPNET,			/* dl_mac_type */		\
227 	0,				/* dl_reserved */		\
228 	0,				/* dl_current_state */		\
229 	sizeof (uint16_t),		/* dl_sap_length */ 		\
230 	DL_CLDLS,			/* dl_service_mode */		\
231 	0,				/* dl_qos_length */		\
232 	0,				/* dl_qos_offset */		\
233 	0,				/* dl_range_length */		\
234 	0,				/* dl_range_offset */		\
235 	DL_STYLE1,			/* dl_provider_style */		\
236 	0,				/* dl_addr_offset */		\
237 	DL_VERSION_2,			/* dl_version */		\
238 	0,				/* dl_brdcst_addr_length */	\
239 	0				/* dl_brdcst_addr_offset */	\
240 }
241 
242 typedef void ipnet_walkfunc_t(const char *, void *, dev_t);
243 
244 extern int	ipnet_client_open(ipnetif_t *, ipnetif_t **);
245 extern void	ipnet_client_close(ipnetif_t *);
246 extern void	ipnet_close_byhandle(ipnetif_t *);
247 extern int	ipnet_get_linkid_byname(const char *, datalink_id_t *,
248     zoneid_t);
249 extern dev_t	ipnet_if_getdev(char *, zoneid_t);
250 extern const char *ipnet_name(ipnetif_t *);
251 extern int	ipnet_open_byname(const char *, ipnetif_t **, zoneid_t);
252 extern int	ipnet_promisc_add(void *, uint_t, void *, uintptr_t *, int);
253 extern void	ipnet_promisc_remove(void *);
254 extern void	ipnet_set_bpfattach(bpf_attach_fn_t, bpf_detach_fn_t,
255     zoneid_t, bpf_itap_fn_t, bpf_provider_reg_fn_t);
256 extern void	ipnet_walk_if(ipnet_walkfunc_t *, void *, zoneid_t);
257 
258 extern bpf_provider_t	bpf_ipnet;
259 
260 #ifdef __cplusplus
261 }
262 #endif
263 
264 #endif /* _INET_IPNET_H */
265