xref: /illumos-gate/usr/src/lib/libilb/common/libilb_impl.h (revision 132157d7fb25c120ae1deca2a65fa7c78e8fcfd0)
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 _LIBILB_IMPL_H
28 #define	_LIBILB_IMPL_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/note.h>
37 #include <netinet/in.h>
38 #include <netdb.h>
39 #include <net/if.h>
40 #include <inet/ilb.h>
41 #include <libilb.h>
42 #include <thread.h>
43 #include <synch.h>
44 
45 #if !defined max
46 #define	max(a, b)	((a) > (b) ? (a) : (b))
47 #endif
48 
49 /* The UNIX domain socket path to talk to ilbd. */
50 #define	SOCKET_PATH	"/var/run/daemon/ilb_sock"
51 
52 /* The max message size for communicating with ilbd */
53 #define	ILBD_MSG_SIZE	102400
54 
55 /*
56  * moral equivalent of ntohl for IPv6 addresses, MSB and LSB (64 bit each),
57  * assign to uint64_t variables
58  */
59 #define	INV6_N2H_MSB64(addr)				\
60 	(((uint64_t)ntohl((addr)->_S6_un._S6_u32[0]) << 32) + 	\
61 	    (ntohl((addr)->_S6_un._S6_u32[1])))
62 
63 #define	INV6_N2H_LSB64(addr)				\
64 	(((uint64_t)ntohl((addr)->_S6_un._S6_u32[2]) << 32) + 	\
65 	    (ntohl((addr)->_S6_un._S6_u32[3])))
66 
67 /*
68  * moral equiv. of htonl of MSB and LSB 64-bit portions to an IPv6 address
69  */
70 #define	INV6_H2N_MSB64(addr, msb)				\
71 	(addr)->_S6_un._S6_u32[0] = htonl((msb) >> 32);		\
72 	(addr)->_S6_un._S6_u32[1] = htonl((msb) & 0xffffffff)
73 
74 #define	INV6_H2N_LSB64(addr, lsb)				\
75 	(addr)->_S6_un._S6_u32[2] = htonl((lsb) >> 32);		\
76 	(addr)->_S6_un._S6_u32[3] = htonl((lsb) & 0xffffffff)
77 
78 #define	IP_COPY_CLI_2_IMPL(_e, _i)				\
79 	bzero(_i, sizeof (*(_i)));				\
80 	if ((_e)->ia_af == AF_INET6)  				\
81 		(void) memcpy((_i), &(_e)->ia_v6, sizeof (*(_i)));	\
82 	else							\
83 		IN6_INADDR_TO_V4MAPPED(&(_e)->ia_v4, (_i))
84 
85 #define	IP_COPY_IMPL_2_CLI(_i, _e)				\
86 	do {							\
87 		bzero(_e, sizeof (*(_e)));			\
88 		if (IN6_IS_ADDR_V4MAPPED(_i)) {			\
89 			(_e)->ia_af = AF_INET;			\
90 			IN6_V4MAPPED_TO_INADDR((_i), &(_e)->ia_v4); \
91 		} else {					\
92 			(_e)->ia_af = AF_INET6;			\
93 			(void) memcpy(&(_e)->ia_v6, (_i), 	\
94 			    sizeof ((_e)->ia_v6));		\
95 		}						\
96 		_NOTE(CONSTCOND)				\
97 	} while (0)
98 
99 #define	GET_AF(_a) IN6_IS_ADDR_V4MAPPED(_a)?AF_INET:AF_INET6
100 #define	IS_AF_VALID(_af) (_af == AF_INET || _af == AF_INET6)
101 
102 typedef enum {
103 	ILBD_BAD_CMD = 0,
104 				/* servergroup commands */
105 	ILBD_CREATE_SERVERGROUP,
106 	ILBD_ADD_SERVER_TO_GROUP,
107 	ILBD_REM_SERVER_FROM_GROUP,
108 	ILBD_ENABLE_SERVER,
109 	ILBD_DISABLE_SERVER,
110 	ILBD_DESTROY_SERVERGROUP,
111 	ILBD_RETRIEVE_SG_NAMES,		/* names of all SGs registered */
112 	ILBD_RETRIEVE_SG_HOSTS,		/* all hosts for a given SG (hndl) */
113 	ILBD_SRV_ADDR2ID,	/* fill in serverID for given address */
114 	ILBD_SRV_ID2ADDR,	/* fill in address from given serverID */
115 				/* rule commands */
116 	ILBD_CREATE_RULE,
117 	ILBD_DESTROY_RULE,
118 	ILBD_ENABLE_RULE,
119 	ILBD_DISABLE_RULE,
120 	ILBD_RETRIEVE_RULE_NAMES,
121 	ILBD_RETRIEVE_RULE,
122 
123 	ILBD_CREATE_HC,
124 	ILBD_DESTROY_HC,
125 	ILBD_GET_HC_INFO,
126 	ILBD_GET_HC_SRVS,
127 	ILBD_GET_HC_RULES,
128 	ILBD_RETRIEVE_HC_NAMES,
129 
130 	ILBD_SHOW_NAT,		/* list the NAT table */
131 	ILBD_SHOW_PERSIST,	/* list the sticky table */
132 
133 	ILBD_CMD_OK,		/* Requested operation succeeds. */
134 	ILBD_CMD_ERROR		/* Rquested operation fails. */
135 } ilbd_cmd_t;
136 
137 typedef struct sg_srv {
138 	int32_t		sgs_flags;	/* enabled, dis- */
139 	struct in6_addr	sgs_addr;
140 	int32_t		sgs_minport;
141 	int32_t		sgs_maxport;
142 	int32_t		sgs_id;		/* numerical part of srvID */
143 	char		sgs_srvID[ILB_NAMESZ];	/* "name" given to server */
144 } ilb_sg_srv_t;
145 
146 typedef struct sg_info {
147 	int32_t		sg_flags;
148 	char		sg_name[ILB_SGNAME_SZ];
149 	int32_t		sg_srvcount;
150 	ilb_sg_srv_t	sg_servers[];
151 } ilb_sg_info_t;
152 
153 typedef char	ilbd_name_t[ILB_NAMESZ];
154 
155 typedef struct ilbd_namelist {
156 	int32_t		ilbl_flags;
157 	int32_t		ilbl_count;
158 	ilbd_name_t	ilbl_name[];
159 } ilbd_namelist_t;
160 
161 #define	ILBL_NAME_OFFSET	(offsetof(ilbd_namelist_t, ilbl_name))
162 
163 typedef struct rule_info {
164 	int32_t		rl_flags;
165 	char		rl_name[ILB_NAMESZ];
166 	struct in6_addr	rl_vip;
167 	uint16_t	rl_proto;
168 	uint16_t	rl_ipversion;
169 	int32_t		rl_minport;
170 	int32_t		rl_maxport;
171 	ilb_algo_t	rl_algo;
172 	ilb_topo_t	rl_topo;
173 	struct in6_addr	rl_nat_src_start;
174 	struct in6_addr	rl_nat_src_end;
175 	struct in6_addr	rl_stickymask;
176 	uint32_t	rl_conndrain;
177 	uint32_t	rl_nat_timeout;
178 	uint32_t	rl_sticky_timeout;
179 	in_port_t	rl_hcport;
180 	ilb_hcp_flags_t	rl_hcpflag;
181 	char		rl_sgname[ILB_SGNAME_SZ];
182 	char		rl_hcname[ILB_NAMESZ];
183 } ilb_rule_info_t;
184 
185 /*
186  * Struct to represent show NAT request and reply.
187  *
188  * sn_num: (request) indicates the number of entries wanted;
189  *         (reply) the number of entries returned;
190  * sn_data: NAT/persist able entries (is uint32_t aligned).
191  */
192 typedef struct {
193 	uint32_t	sn_num;
194 	uint32_t	sn_data[];
195 } ilb_show_info_t;
196 
197 /*
198  * Struct to represent the set of servers associated with a hc object.
199  *
200  * rs_num_srvs: number of servers in this struct.
201  * rs_srvs: array of servers.
202  */
203 typedef struct {
204 	uint32_t	rs_num_srvs;
205 	ilb_hc_srv_t	rs_srvs[];
206 } ilb_hc_rule_srv_t;
207 
208 typedef struct ilb_handle_impl {
209 	mutex_t		h_lock;
210 	cond_t		h_cv;
211 	boolean_t	h_busy;
212 	boolean_t	h_valid;
213 	boolean_t	h_closing;
214 	uint32_t	h_waiter;
215 	int		h_socket;
216 	ilb_status_t	h_error;	/* ... that caused invalidation */
217 } ilb_handle_impl_t;
218 
219 /*
220  * Communication flags used in ilb_comm_t.
221  *
222  * ILB_COMM_END: end of communication
223  */
224 #define	ILB_COMM_END	0x1
225 
226 /*
227  * The message structure used to communicate with ilbd.
228  *
229  * ic_cmd: the message type.
230  * ic_flags: communication flags
231  * ic_data: message data (is uint32_t aligned).
232  */
233 typedef struct {
234 	ilbd_cmd_t	ic_cmd;
235 	int32_t		ic_flags;
236 	uint32_t	ic_data[];
237 } ilb_comm_t;
238 
239 ilb_status_t	i_check_ip_range(ilb_ip_addr_t *, ilb_ip_addr_t *);
240 ilb_status_t	i_ilb_do_comm(ilb_handle_t, ilb_comm_t *, size_t, ilb_comm_t *,
241 		    size_t *);
242 void		i_ilb_close_comm(ilb_handle_t);
243 struct in6_addr	i_next_ip_addr(struct in6_addr *, int);
244 
245 ilb_status_t	i_ilb_retrieve_rule_names(ilb_handle_t, ilb_comm_t **,
246 		    size_t *);
247 ilb_comm_t 	*i_ilb_alloc_req(ilbd_cmd_t, size_t *);
248 
249 #ifdef __cplusplus
250 }
251 #endif
252 
253 #endif /* _LIBILB_IMPL_H */
254