1dbed73cbSSangeeta Misra /*
2dbed73cbSSangeeta Misra  * CDDL HEADER START
3dbed73cbSSangeeta Misra  *
4dbed73cbSSangeeta Misra  * The contents of this file are subject to the terms of the
5dbed73cbSSangeeta Misra  * Common Development and Distribution License (the "License").
6dbed73cbSSangeeta Misra  * You may not use this file except in compliance with the License.
7dbed73cbSSangeeta Misra  *
8dbed73cbSSangeeta Misra  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9dbed73cbSSangeeta Misra  * or http://www.opensolaris.org/os/licensing.
10dbed73cbSSangeeta Misra  * See the License for the specific language governing permissions
11dbed73cbSSangeeta Misra  * and limitations under the License.
12dbed73cbSSangeeta Misra  *
13dbed73cbSSangeeta Misra  * When distributing Covered Code, include this CDDL HEADER in each
14dbed73cbSSangeeta Misra  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15dbed73cbSSangeeta Misra  * If applicable, add the following below this CDDL HEADER, with the
16dbed73cbSSangeeta Misra  * fields enclosed by brackets "[]" replaced with your own identifying
17dbed73cbSSangeeta Misra  * information: Portions Copyright [yyyy] [name of copyright owner]
18dbed73cbSSangeeta Misra  *
19dbed73cbSSangeeta Misra  * CDDL HEADER END
20dbed73cbSSangeeta Misra  */
21dbed73cbSSangeeta Misra 
22dbed73cbSSangeeta Misra /*
233ae6a67dSSangeeta Misra  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24dbed73cbSSangeeta Misra  */
25dbed73cbSSangeeta Misra 
26dbed73cbSSangeeta Misra #include <stdlib.h>
27dbed73cbSSangeeta Misra #include <strings.h>
28dbed73cbSSangeeta Misra #include <stddef.h>
29dbed73cbSSangeeta Misra #include <unistd.h>
30dbed73cbSSangeeta Misra #include <sys/types.h>
31dbed73cbSSangeeta Misra #include <sys/socket.h>
32dbed73cbSSangeeta Misra #include <netinet/in.h>
33dbed73cbSSangeeta Misra #include <arpa/inet.h>
34dbed73cbSSangeeta Misra #include <sys/list.h>
35dbed73cbSSangeeta Misra #include <net/if.h>
36dbed73cbSSangeeta Misra #include <assert.h>
37dbed73cbSSangeeta Misra #include <errno.h>
38dbed73cbSSangeeta Misra #include <libintl.h>
39dbed73cbSSangeeta Misra #include <libilb.h>
40dbed73cbSSangeeta Misra #include <inet/ilb.h>
41dbed73cbSSangeeta Misra #include "libilb_impl.h"
42dbed73cbSSangeeta Misra #include "ilbd.h"
43dbed73cbSSangeeta Misra 
44dbed73cbSSangeeta Misra /* until we all use AF_* macros ... */
45dbed73cbSSangeeta Misra #define	AF_2_IPPROTO(_af)	(_af == AF_INET)?IPPROTO_IP:IPPROTO_IPV6
46dbed73cbSSangeeta Misra #define	IPPROTO_2_AF(_i)	(_i == IPPROTO_IP)?AF_INET:AF_INET6
47dbed73cbSSangeeta Misra 
483ae6a67dSSangeeta Misra #define	PROTOCOL_LEN	16				/* protocol type */
493ae6a67dSSangeeta Misra #define	ADDR_LEN	(2 * INET6_ADDRSTRLEN + 1)	/* prxy src range */
503ae6a67dSSangeeta Misra #define	PORT_LEN	6			/* hcport:1-65535 or "ANY" */
513ae6a67dSSangeeta Misra 
52dbed73cbSSangeeta Misra static ilb_status_t ilbd_disable_one_rule(ilbd_rule_t *, boolean_t);
53dbed73cbSSangeeta Misra static uint32_t i_flags_d2k(int);
54dbed73cbSSangeeta Misra 
55dbed73cbSSangeeta Misra #define	ILB_SGSRV_2_KSRV(s, k)			\
56dbed73cbSSangeeta Misra 	(k)->addr  = (s)->sgs_addr;		\
57dbed73cbSSangeeta Misra 	(k)->min_port = (s)->sgs_minport;	\
58dbed73cbSSangeeta Misra 	(k)->max_port = (s)->sgs_maxport;	\
59dbed73cbSSangeeta Misra 	(k)->flags = i_flags_d2k((s)->sgs_flags);	\
60dbed73cbSSangeeta Misra 	(k)->err = 0;				\
61dbed73cbSSangeeta Misra 	(void) strlcpy((k)->name, (s)->sgs_srvID, sizeof ((k)->name))
62dbed73cbSSangeeta Misra 
63dbed73cbSSangeeta Misra list_t		ilbd_rule_hlist;
64dbed73cbSSangeeta Misra 
65dbed73cbSSangeeta Misra static ilb_algo_t
algo_impl2lib(ilb_algo_impl_t a)66dbed73cbSSangeeta Misra algo_impl2lib(ilb_algo_impl_t a)
67dbed73cbSSangeeta Misra {
68dbed73cbSSangeeta Misra 	switch (a) {
69dbed73cbSSangeeta Misra 	case ILB_ALG_IMPL_ROUNDROBIN:
70dbed73cbSSangeeta Misra 		return (ILB_ALG_ROUNDROBIN);
71dbed73cbSSangeeta Misra 	case ILB_ALG_IMPL_HASH_IP:
72dbed73cbSSangeeta Misra 		return (ILB_ALG_HASH_IP);
73dbed73cbSSangeeta Misra 	case ILB_ALG_IMPL_HASH_IP_SPORT:
74dbed73cbSSangeeta Misra 		return (ILB_ALG_HASH_IP_SPORT);
75dbed73cbSSangeeta Misra 	case ILB_ALG_IMPL_HASH_IP_VIP:
76dbed73cbSSangeeta Misra 		return (ILB_ALG_HASH_IP_VIP);
77dbed73cbSSangeeta Misra 	}
78dbed73cbSSangeeta Misra 	return (0);
79dbed73cbSSangeeta Misra }
80dbed73cbSSangeeta Misra 
81dbed73cbSSangeeta Misra static ilb_topo_t
topo_impl2lib(ilb_topo_impl_t t)82dbed73cbSSangeeta Misra topo_impl2lib(ilb_topo_impl_t t)
83dbed73cbSSangeeta Misra {
84dbed73cbSSangeeta Misra 	switch (t) {
85dbed73cbSSangeeta Misra 	case ILB_TOPO_IMPL_DSR:
86dbed73cbSSangeeta Misra 		return (ILB_TOPO_DSR);
87dbed73cbSSangeeta Misra 	case ILB_TOPO_IMPL_NAT:
88dbed73cbSSangeeta Misra 		return (ILB_TOPO_NAT);
89dbed73cbSSangeeta Misra 	case ILB_TOPO_IMPL_HALF_NAT:
90dbed73cbSSangeeta Misra 		return (ILB_TOPO_HALF_NAT);
91dbed73cbSSangeeta Misra 	}
92dbed73cbSSangeeta Misra 	return (0);
93dbed73cbSSangeeta Misra }
94dbed73cbSSangeeta Misra 
95dbed73cbSSangeeta Misra ilb_algo_impl_t
algo_lib2impl(ilb_algo_t a)96dbed73cbSSangeeta Misra algo_lib2impl(ilb_algo_t a)
97dbed73cbSSangeeta Misra {
98dbed73cbSSangeeta Misra 	switch (a) {
99dbed73cbSSangeeta Misra 	case ILB_ALG_ROUNDROBIN:
100dbed73cbSSangeeta Misra 		return (ILB_ALG_IMPL_ROUNDROBIN);
101dbed73cbSSangeeta Misra 	case ILB_ALG_HASH_IP:
102dbed73cbSSangeeta Misra 		return (ILB_ALG_IMPL_HASH_IP);
103dbed73cbSSangeeta Misra 	case ILB_ALG_HASH_IP_SPORT:
104dbed73cbSSangeeta Misra 		return (ILB_ALG_IMPL_HASH_IP_SPORT);
105dbed73cbSSangeeta Misra 	case ILB_ALG_HASH_IP_VIP:
106dbed73cbSSangeeta Misra 		return (ILB_ALG_IMPL_HASH_IP_VIP);
107dbed73cbSSangeeta Misra 	}
108dbed73cbSSangeeta Misra 	return (0);
109dbed73cbSSangeeta Misra }
110dbed73cbSSangeeta Misra 
111dbed73cbSSangeeta Misra ilb_topo_impl_t
topo_lib2impl(ilb_topo_t t)112dbed73cbSSangeeta Misra topo_lib2impl(ilb_topo_t t)
113dbed73cbSSangeeta Misra {
114dbed73cbSSangeeta Misra 	switch (t) {
115dbed73cbSSangeeta Misra 	case ILB_TOPO_DSR:
116dbed73cbSSangeeta Misra 		return (ILB_TOPO_IMPL_DSR);
117dbed73cbSSangeeta Misra 	case ILB_TOPO_NAT:
118dbed73cbSSangeeta Misra 		return (ILB_TOPO_IMPL_NAT);
119dbed73cbSSangeeta Misra 	case ILB_TOPO_HALF_NAT:
120dbed73cbSSangeeta Misra 		return (ILB_TOPO_IMPL_HALF_NAT);
121dbed73cbSSangeeta Misra 	}
122dbed73cbSSangeeta Misra 	return (0);
123dbed73cbSSangeeta Misra }
124dbed73cbSSangeeta Misra 
125dbed73cbSSangeeta Misra /*
126dbed73cbSSangeeta Misra  * Walk the list of rules and check if its safe to add the
127dbed73cbSSangeeta Misra  * the server to the rule (this is a list of rules hanging
128dbed73cbSSangeeta Misra  * off of a server group)
129dbed73cbSSangeeta Misra  */
130dbed73cbSSangeeta Misra ilb_status_t
i_check_srv2rules(list_t * rlist,ilb_sg_srv_t * srv)131dbed73cbSSangeeta Misra i_check_srv2rules(list_t *rlist, ilb_sg_srv_t *srv)
132dbed73cbSSangeeta Misra {
133dbed73cbSSangeeta Misra 	ilb_status_t	rc = ILB_STATUS_OK;
134dbed73cbSSangeeta Misra 	ilbd_rule_t	*rl;
135dbed73cbSSangeeta Misra 	int		server_portrange, rule_portrange;
136dbed73cbSSangeeta Misra 	int		srv_minport, srv_maxport;
137dbed73cbSSangeeta Misra 	int		r_minport, r_maxport;
138dbed73cbSSangeeta Misra 
139dbed73cbSSangeeta Misra 	if (srv == NULL)
140dbed73cbSSangeeta Misra 		return (ILB_STATUS_OK);
141dbed73cbSSangeeta Misra 
142dbed73cbSSangeeta Misra 	srv_minport = ntohs(srv->sgs_minport);
143dbed73cbSSangeeta Misra 	srv_maxport = ntohs(srv->sgs_maxport);
144dbed73cbSSangeeta Misra 
145dbed73cbSSangeeta Misra 	for (rl = list_head(rlist); rl != NULL; rl = list_next(rlist, rl)) {
146dbed73cbSSangeeta Misra 		r_minport = ntohs(rl->irl_minport);
147dbed73cbSSangeeta Misra 		r_maxport = ntohs(rl->irl_maxport);
148dbed73cbSSangeeta Misra 
149dbed73cbSSangeeta Misra 		if ((srv_minport != 0) && (srv_minport == srv_maxport)) {
150dbed73cbSSangeeta Misra 			/* server has single port */
151dbed73cbSSangeeta Misra 			if (rl->irl_topo == ILB_TOPO_DSR) {
152dbed73cbSSangeeta Misra 				/*
153dbed73cbSSangeeta Misra 				 * either we have a DSR rule with a port
154dbed73cbSSangeeta Misra 				 * range, or both server and rule
155dbed73cbSSangeeta Misra 				 * have single ports but their values
156dbed73cbSSangeeta Misra 				 * don't match - this is incompatible
157dbed73cbSSangeeta Misra 				 */
158dbed73cbSSangeeta Misra 				if (r_maxport > r_minport) {
159dbed73cbSSangeeta Misra 					rc = ILB_STATUS_INVAL_SRVR;
160dbed73cbSSangeeta Misra 					break;
161dbed73cbSSangeeta Misra 				} else if (srv_minport != r_minport) {
162dbed73cbSSangeeta Misra 					rc = ILB_STATUS_BADPORT;
163dbed73cbSSangeeta Misra 					break;
164dbed73cbSSangeeta Misra 				}
165dbed73cbSSangeeta Misra 			}
166dbed73cbSSangeeta Misra 			if (rl->irl_hcpflag == ILB_HCI_PROBE_FIX &&
167dbed73cbSSangeeta Misra 			    rl->irl_hcport != srv_minport) {
168dbed73cbSSangeeta Misra 				rc = ILB_STATUS_BADPORT;
169dbed73cbSSangeeta Misra 				break;
170dbed73cbSSangeeta Misra 			}
171dbed73cbSSangeeta Misra 		} else if (srv_maxport > srv_minport) {
172dbed73cbSSangeeta Misra 			/* server has a port range */
173dbed73cbSSangeeta Misra 			if ((rl->irl_topo == ILB_TOPO_DSR) &&
174dbed73cbSSangeeta Misra 			    (r_maxport > r_minport)) {
175dbed73cbSSangeeta Misra 				if ((r_minport != srv_minport) ||
176dbed73cbSSangeeta Misra 				    (r_maxport != srv_maxport)) {
177dbed73cbSSangeeta Misra 					/*
178dbed73cbSSangeeta Misra 					 * we have a DSR rule with a port range
179dbed73cbSSangeeta Misra 					 * and its min and max port values
180dbed73cbSSangeeta Misra 					 * does not meet that of server's
181dbed73cbSSangeeta Misra 					 * - this is incompatible
182dbed73cbSSangeeta Misra 					 */
183dbed73cbSSangeeta Misra 					rc = ILB_STATUS_BADPORT;
184dbed73cbSSangeeta Misra 					break;
185dbed73cbSSangeeta Misra 				}
186dbed73cbSSangeeta Misra 			} else if ((rl->irl_topo == ILB_TOPO_DSR) &&
187dbed73cbSSangeeta Misra 			    (r_maxport == r_minport)) {
188dbed73cbSSangeeta Misra 					/*
189dbed73cbSSangeeta Misra 					 * we have a DSR rule with a single
190dbed73cbSSangeeta Misra 					 * port and a server with a port range
191dbed73cbSSangeeta Misra 					 * - this is incompatible
192dbed73cbSSangeeta Misra 					 */
193dbed73cbSSangeeta Misra 					rc = ILB_STATUS_INVAL_SRVR;
194dbed73cbSSangeeta Misra 					break;
195dbed73cbSSangeeta Misra 			} else if (((rl->irl_topo == ILB_TOPO_NAT) ||
196dbed73cbSSangeeta Misra 			    (rl->irl_topo == ILB_TOPO_HALF_NAT)) &&
197dbed73cbSSangeeta Misra 			    (r_maxport > r_minport)) {
198dbed73cbSSangeeta Misra 				server_portrange = srv_maxport - srv_minport;
199dbed73cbSSangeeta Misra 				rule_portrange = r_maxport - r_minport;
200dbed73cbSSangeeta Misra 				if (rule_portrange != server_portrange) {
201dbed73cbSSangeeta Misra 					/*
202dbed73cbSSangeeta Misra 					 * we have a NAT/Half-NAT rule with
203dbed73cbSSangeeta Misra 					 * a port range and server with a port
204dbed73cbSSangeeta Misra 					 * range and there is a mismatch in the
205dbed73cbSSangeeta Misra 					 * sizes of the port ranges - this is
206dbed73cbSSangeeta Misra 					 * incompatible
207dbed73cbSSangeeta Misra 					 */
208dbed73cbSSangeeta Misra 					rc = ILB_STATUS_INVAL_SRVR;
209dbed73cbSSangeeta Misra 					break;
210dbed73cbSSangeeta Misra 				}
211dbed73cbSSangeeta Misra 			}
212dbed73cbSSangeeta Misra 			if (rl->irl_hcpflag == ILB_HCI_PROBE_FIX &&
213dbed73cbSSangeeta Misra 			    (rl->irl_hcport > srv_maxport ||
214dbed73cbSSangeeta Misra 			    rl->irl_hcport < srv_minport)) {
215dbed73cbSSangeeta Misra 				rc = ILB_STATUS_BADPORT;
216dbed73cbSSangeeta Misra 				break;
217dbed73cbSSangeeta Misra 			}
218dbed73cbSSangeeta Misra 		}
219dbed73cbSSangeeta Misra 	}
220dbed73cbSSangeeta Misra 
221dbed73cbSSangeeta Misra 	return (rc);
222dbed73cbSSangeeta Misra }
223dbed73cbSSangeeta Misra 
224dbed73cbSSangeeta Misra void
i_setup_rule_hlist(void)225dbed73cbSSangeeta Misra i_setup_rule_hlist(void)
226dbed73cbSSangeeta Misra {
227dbed73cbSSangeeta Misra 	list_create(&ilbd_rule_hlist, sizeof (ilbd_rule_t),
228dbed73cbSSangeeta Misra 	    offsetof(ilbd_rule_t, irl_link));
229dbed73cbSSangeeta Misra }
230dbed73cbSSangeeta Misra 
231dbed73cbSSangeeta Misra ilb_status_t
i_ilbd_save_rule(ilbd_rule_t * irl,ilbd_scf_cmd_t scf_cmd)232dbed73cbSSangeeta Misra i_ilbd_save_rule(ilbd_rule_t *irl, ilbd_scf_cmd_t scf_cmd)
233dbed73cbSSangeeta Misra {
234dbed73cbSSangeeta Misra 	boolean_t enable = irl->irl_flags & ILB_FLAGS_RULE_ENABLED;
235dbed73cbSSangeeta Misra 
236dbed73cbSSangeeta Misra 	switch (scf_cmd) {
237dbed73cbSSangeeta Misra 	case ILBD_SCF_CREATE:
238dbed73cbSSangeeta Misra 		return (ilbd_create_pg(ILBD_SCF_RULE, (void *)irl));
239dbed73cbSSangeeta Misra 	case ILBD_SCF_DESTROY:
240dbed73cbSSangeeta Misra 		return (ilbd_destroy_pg(ILBD_SCF_RULE, irl->irl_name));
241dbed73cbSSangeeta Misra 	case ILBD_SCF_ENABLE_DISABLE:
242dbed73cbSSangeeta Misra 		return (ilbd_change_prop(ILBD_SCF_RULE, irl->irl_name,
243dbed73cbSSangeeta Misra 		    "status", &enable));
244dbed73cbSSangeeta Misra 	default:
245dbed73cbSSangeeta Misra 		logdebug("i_ilbd_save_rule: invalid scf cmd %d", scf_cmd);
246dbed73cbSSangeeta Misra 		return (ILB_STATUS_INVAL_CMD);
247dbed73cbSSangeeta Misra 	}
248dbed73cbSSangeeta Misra }
249dbed73cbSSangeeta Misra 
250dbed73cbSSangeeta Misra /*
251dbed73cbSSangeeta Misra  * allocate a new daemon-specific rule from the "template" passed
252dbed73cbSSangeeta Misra  * in in *r
253dbed73cbSSangeeta Misra  */
254dbed73cbSSangeeta Misra static ilbd_rule_t *
i_alloc_ilbd_rule(ilb_rule_info_t * r)255dbed73cbSSangeeta Misra i_alloc_ilbd_rule(ilb_rule_info_t *r)
256dbed73cbSSangeeta Misra {
257dbed73cbSSangeeta Misra 	ilbd_rule_t	*rl;
258dbed73cbSSangeeta Misra 
259dbed73cbSSangeeta Misra 	rl = calloc(sizeof (*rl), 1);
260dbed73cbSSangeeta Misra 	if (rl != NULL && r != NULL)
261dbed73cbSSangeeta Misra 		bcopy(r, &rl->irl_info, sizeof (*r));
262dbed73cbSSangeeta Misra 
263dbed73cbSSangeeta Misra 	return (rl);
264dbed73cbSSangeeta Misra }
265dbed73cbSSangeeta Misra 
266dbed73cbSSangeeta Misra static ilbd_rule_t *
i_find_rule_byname(const char * name)267dbed73cbSSangeeta Misra i_find_rule_byname(const char *name)
268dbed73cbSSangeeta Misra {
269dbed73cbSSangeeta Misra 	ilbd_rule_t	*rl;
270dbed73cbSSangeeta Misra 
271dbed73cbSSangeeta Misra 	/* find position of rule in list */
272dbed73cbSSangeeta Misra 	rl = list_head(&ilbd_rule_hlist);
273dbed73cbSSangeeta Misra 	while (rl != NULL &&
274dbed73cbSSangeeta Misra 	    strncmp(rl->irl_name, name, sizeof (rl->irl_name)) != 0) {
275dbed73cbSSangeeta Misra 		rl = list_next(&ilbd_rule_hlist, rl);
276dbed73cbSSangeeta Misra 	}
277dbed73cbSSangeeta Misra 
278dbed73cbSSangeeta Misra 	return (rl);
279dbed73cbSSangeeta Misra }
280dbed73cbSSangeeta Misra 
281dbed73cbSSangeeta Misra /*
282dbed73cbSSangeeta Misra  * get exactly one rule (named in rl->irl_name) data from kernel
283dbed73cbSSangeeta Misra  */
284dbed73cbSSangeeta Misra static ilb_status_t
ilb_get_krule(ilb_rule_info_t * rl)285dbed73cbSSangeeta Misra ilb_get_krule(ilb_rule_info_t *rl)
286dbed73cbSSangeeta Misra {
287dbed73cbSSangeeta Misra 	ilb_status_t	rc;
288dbed73cbSSangeeta Misra 	ilb_rule_cmd_t	kcmd;
289dbed73cbSSangeeta Misra 
290dbed73cbSSangeeta Misra 	kcmd.cmd = ILB_LIST_RULE;
291dbed73cbSSangeeta Misra 	(void) strlcpy(kcmd.name, rl->rl_name, sizeof (kcmd.name));
292dbed73cbSSangeeta Misra 	kcmd.flags = 0;
293dbed73cbSSangeeta Misra 
294dbed73cbSSangeeta Misra 	rc = do_ioctl(&kcmd, 0);
295dbed73cbSSangeeta Misra 	if (rc != ILB_STATUS_OK)
296dbed73cbSSangeeta Misra 		return (rc);
297dbed73cbSSangeeta Misra 
298dbed73cbSSangeeta Misra 	rl->rl_flags = kcmd.flags;
299dbed73cbSSangeeta Misra 	rl->rl_ipversion = IPPROTO_2_AF(kcmd.ip_ver);
300dbed73cbSSangeeta Misra 	rl->rl_vip = kcmd.vip;
301dbed73cbSSangeeta Misra 	rl->rl_proto = kcmd.proto;
302dbed73cbSSangeeta Misra 	rl->rl_minport = kcmd.min_port;
303dbed73cbSSangeeta Misra 	rl->rl_maxport = kcmd.max_port;
304dbed73cbSSangeeta Misra 	rl->rl_algo = algo_impl2lib(kcmd.algo);
305dbed73cbSSangeeta Misra 	rl->rl_topo = topo_impl2lib(kcmd.topo);
306dbed73cbSSangeeta Misra 	rl->rl_stickymask = kcmd.sticky_mask;
307dbed73cbSSangeeta Misra 	rl->rl_nat_src_start = kcmd.nat_src_start;
308dbed73cbSSangeeta Misra 	rl->rl_nat_src_end = kcmd.nat_src_end;
309dbed73cbSSangeeta Misra 	(void) strlcpy(rl->rl_name, kcmd.name, sizeof (rl->rl_name));
310dbed73cbSSangeeta Misra 	rl->rl_conndrain = kcmd.conn_drain_timeout;
311dbed73cbSSangeeta Misra 	rl->rl_nat_timeout = kcmd.nat_expiry;
312dbed73cbSSangeeta Misra 	rl->rl_sticky_timeout = kcmd.sticky_expiry;
313dbed73cbSSangeeta Misra 
314dbed73cbSSangeeta Misra 	return (ILB_STATUS_OK);
315dbed73cbSSangeeta Misra }
316dbed73cbSSangeeta Misra 
317dbed73cbSSangeeta Misra ilb_status_t
ilbd_retrieve_rule(ilbd_name_t rl_name,uint32_t * rbuf,size_t * rbufsz)318dbed73cbSSangeeta Misra ilbd_retrieve_rule(ilbd_name_t rl_name, uint32_t *rbuf, size_t *rbufsz)
319dbed73cbSSangeeta Misra {
320dbed73cbSSangeeta Misra 	ilbd_rule_t	*irl = NULL;
321dbed73cbSSangeeta Misra 	ilb_status_t	rc;
322dbed73cbSSangeeta Misra 	ilb_rule_info_t	*rinfo;
323dbed73cbSSangeeta Misra 
324dbed73cbSSangeeta Misra 	irl = i_find_rule_byname(rl_name);
325dbed73cbSSangeeta Misra 	if (irl == NULL)
326dbed73cbSSangeeta Misra 		return (ILB_STATUS_ENOENT);
327dbed73cbSSangeeta Misra 
328dbed73cbSSangeeta Misra 	ilbd_reply_ok(rbuf, rbufsz);
329dbed73cbSSangeeta Misra 	rinfo = (ilb_rule_info_t *)&((ilb_comm_t *)rbuf)->ic_data;
330dbed73cbSSangeeta Misra 	bcopy(&irl->irl_info, rinfo, sizeof (*rinfo));
331dbed73cbSSangeeta Misra 
332dbed73cbSSangeeta Misra 	/*
333dbed73cbSSangeeta Misra 	 * Check if the various timeout values are 0.  If one is, get the
334dbed73cbSSangeeta Misra 	 * default values from kernel.
335dbed73cbSSangeeta Misra 	 */
336dbed73cbSSangeeta Misra 	if (rinfo->rl_conndrain == 0 || rinfo->rl_nat_timeout == 0 ||
337dbed73cbSSangeeta Misra 	    rinfo->rl_sticky_timeout == 0) {
338dbed73cbSSangeeta Misra 		ilb_rule_info_t tmp_info;
339dbed73cbSSangeeta Misra 
340dbed73cbSSangeeta Misra 		(void) strcpy(tmp_info.rl_name, rinfo->rl_name);
341dbed73cbSSangeeta Misra 		rc = ilb_get_krule(&tmp_info);
342dbed73cbSSangeeta Misra 		if (rc != ILB_STATUS_OK)
343dbed73cbSSangeeta Misra 			return (rc);
344dbed73cbSSangeeta Misra 		if (rinfo->rl_conndrain == 0)
345dbed73cbSSangeeta Misra 			rinfo->rl_conndrain = tmp_info.rl_conndrain;
3462c2d21e9SRichard Lowe 		if ((rinfo->rl_topo == ILB_TOPO_NAT ||
3472c2d21e9SRichard Lowe 		    rinfo->rl_topo == ILB_TOPO_HALF_NAT) &&
348dbed73cbSSangeeta Misra 		    rinfo->rl_nat_timeout == 0) {
349dbed73cbSSangeeta Misra 			rinfo->rl_nat_timeout = tmp_info.rl_nat_timeout;
350dbed73cbSSangeeta Misra 		}
351dbed73cbSSangeeta Misra 		if ((rinfo->rl_flags & ILB_FLAGS_RULE_STICKY) &&
352dbed73cbSSangeeta Misra 		    rinfo->rl_sticky_timeout == 0) {
353dbed73cbSSangeeta Misra 			rinfo->rl_sticky_timeout = tmp_info.rl_sticky_timeout;
354dbed73cbSSangeeta Misra 		}
355dbed73cbSSangeeta Misra 	}
356dbed73cbSSangeeta Misra 	*rbufsz += sizeof (ilb_rule_info_t);
357dbed73cbSSangeeta Misra 
358dbed73cbSSangeeta Misra 	return (ILB_STATUS_OK);
359dbed73cbSSangeeta Misra }
360dbed73cbSSangeeta Misra 
361dbed73cbSSangeeta Misra static ilb_status_t
ilbd_destroy_one_rule(ilbd_rule_t * irl)362dbed73cbSSangeeta Misra ilbd_destroy_one_rule(ilbd_rule_t *irl)
363dbed73cbSSangeeta Misra {
364dbed73cbSSangeeta Misra 	ilb_status_t	rc;
365dbed73cbSSangeeta Misra 	ilb_name_cmd_t	kcmd;
366dbed73cbSSangeeta Misra 
367dbed73cbSSangeeta Misra 	/*
368dbed73cbSSangeeta Misra 	 * as far as talking to the kernel is concerned, "all rules"
369dbed73cbSSangeeta Misra 	 * is handled in one go somewhere else, so we only
370dbed73cbSSangeeta Misra 	 * tell the kernel about single rules here.
371dbed73cbSSangeeta Misra 	 */
372dbed73cbSSangeeta Misra 	if ((irl->irl_flags & ILB_FLAGS_RULE_ALLRULES) == 0) {
373dbed73cbSSangeeta Misra 		kcmd.cmd = ILB_DESTROY_RULE;
374dbed73cbSSangeeta Misra 		(void) strlcpy(kcmd.name, irl->irl_name, sizeof (kcmd.name));
375dbed73cbSSangeeta Misra 		kcmd.flags = 0;
376dbed73cbSSangeeta Misra 
377dbed73cbSSangeeta Misra 		rc = do_ioctl(&kcmd, 0);
378dbed73cbSSangeeta Misra 		if (rc != ILB_STATUS_OK)
379dbed73cbSSangeeta Misra 			return (rc);
380dbed73cbSSangeeta Misra 
381dbed73cbSSangeeta Misra 	}
382dbed73cbSSangeeta Misra 	list_remove(&irl->irl_sg->isg_rulelist, irl);
383dbed73cbSSangeeta Misra 	list_remove(&ilbd_rule_hlist, irl);
384dbed73cbSSangeeta Misra 
385dbed73cbSSangeeta Misra 	/*
386dbed73cbSSangeeta Misra 	 * When dissociating a rule, only two errors can happen.  The hc
387dbed73cbSSangeeta Misra 	 * name is incorrect or the rule is not associated with the hc
388dbed73cbSSangeeta Misra 	 * object.  Both should not happen....  The check is for debugging
389dbed73cbSSangeeta Misra 	 * purpose.
390dbed73cbSSangeeta Misra 	 */
391dbed73cbSSangeeta Misra 	if (RULE_HAS_HC(irl) && (rc = ilbd_hc_dissociate_rule(irl)) !=
392dbed73cbSSangeeta Misra 	    ILB_STATUS_OK) {
393dbed73cbSSangeeta Misra 		logerr("ilbd_destroy_one_rule: cannot "
394dbed73cbSSangeeta Misra 		    "dissociate %s from hc object %s: %d",
395dbed73cbSSangeeta Misra 		    irl->irl_name, irl->irl_hcname, rc);
396dbed73cbSSangeeta Misra 	}
397dbed73cbSSangeeta Misra 
398dbed73cbSSangeeta Misra 	rc = i_ilbd_save_rule(irl, ILBD_SCF_DESTROY);
399dbed73cbSSangeeta Misra 	if (rc != ILB_STATUS_OK)
400dbed73cbSSangeeta Misra 		logdebug("ilbd_destroy_rule: save rule failed");
401dbed73cbSSangeeta Misra 
402dbed73cbSSangeeta Misra 	free(irl);
403dbed73cbSSangeeta Misra 	return (rc);
404dbed73cbSSangeeta Misra }
405dbed73cbSSangeeta Misra 
406dbed73cbSSangeeta Misra /*
407dbed73cbSSangeeta Misra  * the following two functions are the other's opposite, and can
408dbed73cbSSangeeta Misra  * call into each other for roll back purposes in case of error.
409dbed73cbSSangeeta Misra  * To avoid endless recursion, the 'is_rollback' parameter must be
410dbed73cbSSangeeta Misra  * set to B_TRUE in the roll back case.
411dbed73cbSSangeeta Misra  */
412dbed73cbSSangeeta Misra static ilb_status_t
ilbd_enable_one_rule(ilbd_rule_t * irl,boolean_t is_rollback)413dbed73cbSSangeeta Misra ilbd_enable_one_rule(ilbd_rule_t *irl, boolean_t is_rollback)
414dbed73cbSSangeeta Misra {
415dbed73cbSSangeeta Misra 	ilb_status_t	rc = ILB_STATUS_OK;
416dbed73cbSSangeeta Misra 	ilb_name_cmd_t	kcmd;
417dbed73cbSSangeeta Misra 
418dbed73cbSSangeeta Misra 	/* no use sending a no-op to the kernel */
419dbed73cbSSangeeta Misra 	if ((irl->irl_flags & ILB_FLAGS_RULE_ENABLED) != 0)
420dbed73cbSSangeeta Misra 		return (ILB_STATUS_OK);
421dbed73cbSSangeeta Misra 
422dbed73cbSSangeeta Misra 	irl->irl_flags |= ILB_FLAGS_RULE_ENABLED;
423dbed73cbSSangeeta Misra 
424dbed73cbSSangeeta Misra 	/* "all rules" is handled in one go somewhere else, not here */
425dbed73cbSSangeeta Misra 	if ((irl->irl_flags & ILB_FLAGS_RULE_ALLRULES) == 0) {
426dbed73cbSSangeeta Misra 		kcmd.cmd = ILB_ENABLE_RULE;
427dbed73cbSSangeeta Misra 		(void) strlcpy(kcmd.name, irl->irl_name, sizeof (kcmd.name));
428dbed73cbSSangeeta Misra 		kcmd.flags = 0;
429dbed73cbSSangeeta Misra 
430dbed73cbSSangeeta Misra 		rc = do_ioctl(&kcmd, 0);
431dbed73cbSSangeeta Misra 		if (rc != ILB_STATUS_OK)
432dbed73cbSSangeeta Misra 			return (rc);
433dbed73cbSSangeeta Misra 	}
434dbed73cbSSangeeta Misra 	if (RULE_HAS_HC(irl) && (rc = ilbd_hc_enable_rule(irl)) !=
435dbed73cbSSangeeta Misra 	    ILB_STATUS_OK) {
436dbed73cbSSangeeta Misra 		/* Undo the kernel work */
437dbed73cbSSangeeta Misra 		kcmd.cmd = ILB_DISABLE_RULE;
438dbed73cbSSangeeta Misra 		/* Cannot do much if ioctl fails... */
439dbed73cbSSangeeta Misra 		(void) do_ioctl(&kcmd, 0);
440dbed73cbSSangeeta Misra 		return (rc);
441dbed73cbSSangeeta Misra 	}
442dbed73cbSSangeeta Misra 
443dbed73cbSSangeeta Misra 	if (!is_rollback) {
444dbed73cbSSangeeta Misra 		if (rc == ILB_STATUS_OK)
445dbed73cbSSangeeta Misra 			rc = i_ilbd_save_rule(irl, ILBD_SCF_ENABLE_DISABLE);
446dbed73cbSSangeeta Misra 		if (rc != ILB_STATUS_OK)
447dbed73cbSSangeeta Misra 			/* ignore rollback return code */
448dbed73cbSSangeeta Misra 			(void) ilbd_disable_one_rule(irl, B_TRUE);
449dbed73cbSSangeeta Misra 	}
450dbed73cbSSangeeta Misra 
451dbed73cbSSangeeta Misra 	return (rc);
452dbed73cbSSangeeta Misra }
453dbed73cbSSangeeta Misra 
454dbed73cbSSangeeta Misra static ilb_status_t
ilbd_disable_one_rule(ilbd_rule_t * irl,boolean_t is_rollback)455dbed73cbSSangeeta Misra ilbd_disable_one_rule(ilbd_rule_t *irl, boolean_t is_rollback)
456dbed73cbSSangeeta Misra {
457dbed73cbSSangeeta Misra 	ilb_status_t	rc = ILB_STATUS_OK;
458dbed73cbSSangeeta Misra 	ilb_name_cmd_t	kcmd;
459dbed73cbSSangeeta Misra 
460dbed73cbSSangeeta Misra 	/* no use sending a no-op to the kernel */
461dbed73cbSSangeeta Misra 	if ((irl->irl_flags & ILB_FLAGS_RULE_ENABLED) == 0)
462dbed73cbSSangeeta Misra 		return (ILB_STATUS_OK);
463dbed73cbSSangeeta Misra 
464dbed73cbSSangeeta Misra 	irl->irl_flags &= ~ILB_FLAGS_RULE_ENABLED;
465dbed73cbSSangeeta Misra 
466dbed73cbSSangeeta Misra 	/* "all rules" is handled in one go somewhere else, not here */
467dbed73cbSSangeeta Misra 	if ((irl->irl_flags & ILB_FLAGS_RULE_ALLRULES) == 0) {
468dbed73cbSSangeeta Misra 		kcmd.cmd = ILB_DISABLE_RULE;
469dbed73cbSSangeeta Misra 		(void) strlcpy(kcmd.name, irl->irl_name, sizeof (kcmd.name));
470dbed73cbSSangeeta Misra 		kcmd.flags = 0;
471dbed73cbSSangeeta Misra 
472dbed73cbSSangeeta Misra 		rc = do_ioctl(&kcmd, 0);
473dbed73cbSSangeeta Misra 		if (rc != ILB_STATUS_OK)
474dbed73cbSSangeeta Misra 			return (rc);
475dbed73cbSSangeeta Misra 	}
476dbed73cbSSangeeta Misra 
477dbed73cbSSangeeta Misra 	if (RULE_HAS_HC(irl) && (rc = ilbd_hc_disable_rule(irl)) !=
478dbed73cbSSangeeta Misra 	    ILB_STATUS_OK) {
479dbed73cbSSangeeta Misra 		/* Undo the kernel work */
480dbed73cbSSangeeta Misra 		kcmd.cmd = ILB_ENABLE_RULE;
481dbed73cbSSangeeta Misra 		/* Cannot do much if ioctl fails... */
482dbed73cbSSangeeta Misra 		(void) do_ioctl(&kcmd, 0);
483dbed73cbSSangeeta Misra 		return (rc);
484dbed73cbSSangeeta Misra 	}
485dbed73cbSSangeeta Misra 
486dbed73cbSSangeeta Misra 	if (!is_rollback) {
487dbed73cbSSangeeta Misra 		if (rc == ILB_STATUS_OK)
488dbed73cbSSangeeta Misra 			rc = i_ilbd_save_rule(irl, ILBD_SCF_ENABLE_DISABLE);
489dbed73cbSSangeeta Misra 		if (rc != ILB_STATUS_OK)
490dbed73cbSSangeeta Misra 			/* ignore rollback return code */
491dbed73cbSSangeeta Misra 			(void) ilbd_enable_one_rule(irl, B_TRUE);
492dbed73cbSSangeeta Misra 	}
493dbed73cbSSangeeta Misra 
494dbed73cbSSangeeta Misra 	return (rc);
495dbed73cbSSangeeta Misra }
496dbed73cbSSangeeta Misra 
497dbed73cbSSangeeta Misra /*
498dbed73cbSSangeeta Misra  * Generates an audit record for a supplied rule name
499dbed73cbSSangeeta Misra  * Used for enable_rule, disable_rule, delete_rule,
500dbed73cbSSangeeta Misra  * and create_rule subcommands
501dbed73cbSSangeeta Misra  */
502dbed73cbSSangeeta Misra static void
ilbd_audit_rule_event(const char * audit_rule_name,ilb_rule_info_t * rlinfo,ilbd_cmd_t cmd,ilb_status_t rc,ucred_t * ucredp)503dbed73cbSSangeeta Misra ilbd_audit_rule_event(const char *audit_rule_name,
504dbed73cbSSangeeta Misra     ilb_rule_info_t *rlinfo, ilbd_cmd_t cmd, ilb_status_t rc,
505dbed73cbSSangeeta Misra     ucred_t *ucredp)
506dbed73cbSSangeeta Misra {
507dbed73cbSSangeeta Misra 	adt_session_data_t	*ah;
508dbed73cbSSangeeta Misra 	adt_event_data_t	*event;
509dbed73cbSSangeeta Misra 	au_event_t		flag;
510dbed73cbSSangeeta Misra 	int			scf_val_len = ILBD_MAX_VALUE_LEN;
5113ae6a67dSSangeeta Misra 	char			*aobuf = NULL; /* algo:topo */
5123ae6a67dSSangeeta Misra 	char			*valstr1 = NULL;
5133ae6a67dSSangeeta Misra 	char			*valstr2 = NULL;
5143ae6a67dSSangeeta Misra 	char			pbuf[PROTOCOL_LEN]; /* protocol */
5153ae6a67dSSangeeta Misra 	char			hcpbuf[PORT_LEN]; /* hcport */
516dbed73cbSSangeeta Misra 	int			audit_error;
517dbed73cbSSangeeta Misra 
518dbed73cbSSangeeta Misra 	if ((ucredp == NULL) && (cmd == ILBD_CREATE_RULE))  {
519dbed73cbSSangeeta Misra 		/*
520dbed73cbSSangeeta Misra 		 * we came here from the path where ilbd incorporates
521dbed73cbSSangeeta Misra 		 * the configuration that is listed in SCF :
522dbed73cbSSangeeta Misra 		 * i_ilbd_read_config->ilbd_walk_rule_pgs->
523dbed73cbSSangeeta Misra 		 *    ->ilbd_scf_instance_walk_pg->ilbd_create_rule
524dbed73cbSSangeeta Misra 		 * We skip auditing in that case
525dbed73cbSSangeeta Misra 		 */
526dbed73cbSSangeeta Misra 		return;
527dbed73cbSSangeeta Misra 	}
528dbed73cbSSangeeta Misra 	if (adt_start_session(&ah, NULL, 0) != 0) {
529dbed73cbSSangeeta Misra 		logerr("ilbd_audit_rule_event: adt_start_session failed");
530dbed73cbSSangeeta Misra 		exit(EXIT_FAILURE);
531dbed73cbSSangeeta Misra 	}
532dbed73cbSSangeeta Misra 	if (adt_set_from_ucred(ah, ucredp, ADT_NEW) != 0) {
533dbed73cbSSangeeta Misra 		(void) adt_end_session(ah);
534dbed73cbSSangeeta Misra 		logerr("ilbd_audit_rule_event: adt_set_from_ucred failed");
535dbed73cbSSangeeta Misra 		exit(EXIT_FAILURE);
536dbed73cbSSangeeta Misra 	}
537dbed73cbSSangeeta Misra 	if (cmd == ILBD_ENABLE_RULE)
538dbed73cbSSangeeta Misra 		flag = ADT_ilb_enable_rule;
539dbed73cbSSangeeta Misra 	else if (cmd == ILBD_DISABLE_RULE)
540dbed73cbSSangeeta Misra 		flag = ADT_ilb_disable_rule;
541dbed73cbSSangeeta Misra 	else if (cmd == ILBD_DESTROY_RULE)
542dbed73cbSSangeeta Misra 		flag = ADT_ilb_delete_rule;
543dbed73cbSSangeeta Misra 	else if (cmd == ILBD_CREATE_RULE)
544dbed73cbSSangeeta Misra 		flag = ADT_ilb_create_rule;
545dbed73cbSSangeeta Misra 
546dbed73cbSSangeeta Misra 	if ((event = adt_alloc_event(ah, flag)) == NULL) {
547dbed73cbSSangeeta Misra 		logerr("ilbd_audit_rule_event: adt_alloc_event failed");
548dbed73cbSSangeeta Misra 		exit(EXIT_FAILURE);
549dbed73cbSSangeeta Misra 	}
550dbed73cbSSangeeta Misra 
551dbed73cbSSangeeta Misra 	(void) memset((char *)event, 0, sizeof (adt_event_data_t));
552dbed73cbSSangeeta Misra 
553dbed73cbSSangeeta Misra 	switch (cmd) {
554dbed73cbSSangeeta Misra 	case ILBD_DESTROY_RULE:
555dbed73cbSSangeeta Misra 		event->adt_ilb_delete_rule.auth_used = NET_ILB_CONFIG_AUTH;
556dbed73cbSSangeeta Misra 		event->adt_ilb_delete_rule.rule_name = (char *)audit_rule_name;
557dbed73cbSSangeeta Misra 		break;
558dbed73cbSSangeeta Misra 	case ILBD_ENABLE_RULE:
559dbed73cbSSangeeta Misra 		event->adt_ilb_enable_rule.auth_used = NET_ILB_ENABLE_AUTH;
560dbed73cbSSangeeta Misra 		event->adt_ilb_enable_rule.rule_name = (char *)audit_rule_name;
561dbed73cbSSangeeta Misra 		break;
562dbed73cbSSangeeta Misra 	case ILBD_DISABLE_RULE:
563dbed73cbSSangeeta Misra 		event->adt_ilb_disable_rule.auth_used = NET_ILB_ENABLE_AUTH;
564dbed73cbSSangeeta Misra 		event->adt_ilb_disable_rule.rule_name = (char *)audit_rule_name;
565dbed73cbSSangeeta Misra 		break;
566dbed73cbSSangeeta Misra 	case ILBD_CREATE_RULE:
5673ae6a67dSSangeeta Misra 		if (((aobuf = malloc(scf_val_len)) == NULL) ||
5683ae6a67dSSangeeta Misra 		    ((valstr1 = malloc(scf_val_len)) == NULL) ||
5693ae6a67dSSangeeta Misra 		    ((valstr2 = malloc(scf_val_len)) == NULL)) {
5703ae6a67dSSangeeta Misra 			logerr("ilbd_audit_rule_event: could not"
5713ae6a67dSSangeeta Misra 			    " allocate buffer");
5723ae6a67dSSangeeta Misra 			exit(EXIT_FAILURE);
5733ae6a67dSSangeeta Misra 		}
574dbed73cbSSangeeta Misra 
575dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.auth_used = NET_ILB_CONFIG_AUTH;
576dbed73cbSSangeeta Misra 
5775df82708SSangeeta Misra 		/* Fill in virtual IP address type */
5785df82708SSangeeta Misra 		if (IN6_IS_ADDR_V4MAPPED(&rlinfo->rl_vip)) {
5795df82708SSangeeta Misra 			event->adt_ilb_create_rule.virtual_ipaddress_type =
5805df82708SSangeeta Misra 			    ADT_IPv4;
5815df82708SSangeeta Misra 			cvt_addr(event->adt_ilb_create_rule.virtual_ipaddress,
5825df82708SSangeeta Misra 			    ADT_IPv4, rlinfo->rl_vip);
5835df82708SSangeeta Misra 		} else {
5845df82708SSangeeta Misra 			event->adt_ilb_create_rule.virtual_ipaddress_type =
5855df82708SSangeeta Misra 			    ADT_IPv6;
5865df82708SSangeeta Misra 			cvt_addr(event->adt_ilb_create_rule.virtual_ipaddress,
5875df82708SSangeeta Misra 			    ADT_IPv6, rlinfo->rl_vip);
5885df82708SSangeeta Misra 		}
589dbed73cbSSangeeta Misra 		/* Fill in port - could be a single value or a range */
590dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.min_port = ntohs(rlinfo->rl_minport);
591dbed73cbSSangeeta Misra 		if (ntohs(rlinfo->rl_maxport) > ntohs(rlinfo->rl_minport)) {
592dbed73cbSSangeeta Misra 			/* port range */
593dbed73cbSSangeeta Misra 			event->adt_ilb_create_rule.max_port =
594dbed73cbSSangeeta Misra 			    ntohs(rlinfo->rl_maxport);
595dbed73cbSSangeeta Misra 		} else {
596dbed73cbSSangeeta Misra 			/* in audit record, max=min when single port */
597dbed73cbSSangeeta Misra 			event->adt_ilb_create_rule.max_port =
598dbed73cbSSangeeta Misra 			    ntohs(rlinfo->rl_minport);
599dbed73cbSSangeeta Misra 		}
600dbed73cbSSangeeta Misra 
601dbed73cbSSangeeta Misra 		/*
602dbed73cbSSangeeta Misra 		 * Fill in  protocol - if user does not specify it,
603dbed73cbSSangeeta Misra 		 * its TCP by default
604dbed73cbSSangeeta Misra 		 */
605dbed73cbSSangeeta Misra 		if (rlinfo->rl_proto == IPPROTO_UDP)
6063ae6a67dSSangeeta Misra 			(void) snprintf(pbuf, PROTOCOL_LEN, "UDP");
607dbed73cbSSangeeta Misra 		else
6083ae6a67dSSangeeta Misra 			(void) snprintf(pbuf, PROTOCOL_LEN, "TCP");
609dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.protocol = pbuf;
610dbed73cbSSangeeta Misra 
611dbed73cbSSangeeta Misra 		/* Fill in algorithm and operation type */
612dbed73cbSSangeeta Misra 		ilbd_algo_to_str(rlinfo->rl_algo, valstr1);
613dbed73cbSSangeeta Misra 		ilbd_topo_to_str(rlinfo->rl_topo, valstr2);
6143ae6a67dSSangeeta Misra 		(void) snprintf(aobuf, scf_val_len, "%s:%s",
615dbed73cbSSangeeta Misra 		    valstr1, valstr2);
616dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.algo_optype = aobuf;
617dbed73cbSSangeeta Misra 
618dbed73cbSSangeeta Misra 		/* Fill in proxy-src for the NAT case */
619dbed73cbSSangeeta Misra 		if (rlinfo->rl_topo == ILB_TOPO_NAT)  {
6205df82708SSangeeta Misra 			/* copy starting proxy-src address */
6215df82708SSangeeta Misra 			if (IN6_IS_ADDR_V4MAPPED(&rlinfo->rl_nat_src_start)) {
6225df82708SSangeeta Misra 				/* V4 case */
6235df82708SSangeeta Misra 				event->adt_ilb_create_rule.proxy_src_min_type =
6245df82708SSangeeta Misra 				    ADT_IPv4;
6255df82708SSangeeta Misra 				cvt_addr(
6265df82708SSangeeta Misra 				    event->adt_ilb_create_rule.proxy_src_min,
6275df82708SSangeeta Misra 				    ADT_IPv4, rlinfo->rl_nat_src_start);
6285df82708SSangeeta Misra 			} else {
6295df82708SSangeeta Misra 				/* V6 case */
6305df82708SSangeeta Misra 				event->adt_ilb_create_rule.proxy_src_min_type =
6315df82708SSangeeta Misra 				    ADT_IPv6;
6325df82708SSangeeta Misra 				cvt_addr(
6335df82708SSangeeta Misra 				    event->adt_ilb_create_rule.proxy_src_min,
6345df82708SSangeeta Misra 				    ADT_IPv6, rlinfo->rl_nat_src_start);
6355df82708SSangeeta Misra 			}
6365df82708SSangeeta Misra 
6375df82708SSangeeta Misra 			/* copy ending proxy-src address */
638dbed73cbSSangeeta Misra 			if (&rlinfo->rl_nat_src_end == 0) {
6395df82708SSangeeta Misra 				/* proxy-src is a single address */
6405df82708SSangeeta Misra 				event->adt_ilb_create_rule.proxy_src_max_type =
6415df82708SSangeeta Misra 				    event->
6425df82708SSangeeta Misra 				    adt_ilb_create_rule.proxy_src_min_type;
6435df82708SSangeeta Misra 				(void) memcpy(
6445df82708SSangeeta Misra 				    event->adt_ilb_create_rule.proxy_src_max,
6455df82708SSangeeta Misra 				    event->adt_ilb_create_rule.proxy_src_min,
6465df82708SSangeeta Misra 				    (4 * sizeof (uint32_t)));
6475df82708SSangeeta Misra 			} else if (
6485df82708SSangeeta Misra 			    IN6_IS_ADDR_V4MAPPED(&rlinfo->rl_nat_src_end)) {
6495df82708SSangeeta Misra 				/*
6505df82708SSangeeta Misra 				 * proxy-src is a address range - copy ending
6515df82708SSangeeta Misra 				 * proxy-src address
6525df82708SSangeeta Misra 				 * V4 case
6535df82708SSangeeta Misra 				 */
6545df82708SSangeeta Misra 				event->adt_ilb_create_rule.proxy_src_max_type =
6555df82708SSangeeta Misra 				    ADT_IPv4;
6565df82708SSangeeta Misra 				cvt_addr(
6575df82708SSangeeta Misra 				    event->adt_ilb_create_rule.proxy_src_max,
6585df82708SSangeeta Misra 				    ADT_IPv4, rlinfo->rl_nat_src_end);
659dbed73cbSSangeeta Misra 			} else {
6605df82708SSangeeta Misra 				/* V6 case */
6615df82708SSangeeta Misra 				event->adt_ilb_create_rule.proxy_src_max_type =
6625df82708SSangeeta Misra 				    ADT_IPv6;
6635df82708SSangeeta Misra 				cvt_addr(
6645df82708SSangeeta Misra 				    event->adt_ilb_create_rule.proxy_src_max,
6655df82708SSangeeta Misra 				    ADT_IPv6, rlinfo->rl_nat_src_end);
666dbed73cbSSangeeta Misra 			}
667dbed73cbSSangeeta Misra 		}
668dbed73cbSSangeeta Misra 
669dbed73cbSSangeeta Misra 		/*
670dbed73cbSSangeeta Misra 		 * Fill in pmask if user has specified one - 0 means
671dbed73cbSSangeeta Misra 		 * no persistence
672dbed73cbSSangeeta Misra 		 */
673dbed73cbSSangeeta Misra 		valstr1[0] = '\0';
674dbed73cbSSangeeta Misra 		ilbd_ip_to_str(rlinfo->rl_ipversion, &rlinfo->rl_stickymask,
675dbed73cbSSangeeta Misra 		    valstr1);
676dbed73cbSSangeeta Misra 			event->adt_ilb_create_rule.persist_mask = valstr1;
677dbed73cbSSangeeta Misra 
678dbed73cbSSangeeta Misra 		/* If there is a hcname */
679dbed73cbSSangeeta Misra 		if (rlinfo->rl_hcname[0] != '\0')
680dbed73cbSSangeeta Misra 			event->adt_ilb_create_rule.hcname = rlinfo->rl_hcname;
681dbed73cbSSangeeta Misra 
682dbed73cbSSangeeta Misra 		/* Fill in hcport */
683dbed73cbSSangeeta Misra 		if (rlinfo->rl_hcpflag == ILB_HCI_PROBE_FIX) {
684dbed73cbSSangeeta Misra 			/* hcport is specified by user */
6853ae6a67dSSangeeta Misra 			(void) snprintf(hcpbuf, PORT_LEN, "%d",
686dbed73cbSSangeeta Misra 			    rlinfo->rl_hcport);
687dbed73cbSSangeeta Misra 			event->adt_ilb_create_rule.hcport = hcpbuf;
688dbed73cbSSangeeta Misra 		} else if (rlinfo->rl_hcpflag == ILB_HCI_PROBE_ANY) {
689dbed73cbSSangeeta Misra 			/* user has specified "ANY" */
6903ae6a67dSSangeeta Misra 			(void) snprintf(hcpbuf, PORT_LEN, "ANY");
691dbed73cbSSangeeta Misra 			event->adt_ilb_create_rule.hcport = hcpbuf;
692dbed73cbSSangeeta Misra 		}
693dbed73cbSSangeeta Misra 		/*
694dbed73cbSSangeeta Misra 		 * Fill out the conndrain, nat_timeout and persist_timeout
695dbed73cbSSangeeta Misra 		 * If the user does not specify them, the default value
696dbed73cbSSangeeta Misra 		 * is set in the kernel. Userland does not know what
697dbed73cbSSangeeta Misra 		 * the values are. So if the user
698dbed73cbSSangeeta Misra 		 * does not specify these values they will show up as
699dbed73cbSSangeeta Misra 		 * 0 in the audit record.
700dbed73cbSSangeeta Misra 		 */
701dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.conndrain_timeout =
702dbed73cbSSangeeta Misra 		    rlinfo->rl_conndrain;
703dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.nat_timeout =
704dbed73cbSSangeeta Misra 		    rlinfo->rl_nat_timeout;
705dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.persist_timeout =
706dbed73cbSSangeeta Misra 		    rlinfo->rl_sticky_timeout;
707dbed73cbSSangeeta Misra 
708dbed73cbSSangeeta Misra 		/* Fill out servergroup and rule name */
709dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.server_group = rlinfo->rl_sgname;
710dbed73cbSSangeeta Misra 		event->adt_ilb_create_rule.rule_name = rlinfo->rl_name;
711dbed73cbSSangeeta Misra 		break;
712dbed73cbSSangeeta Misra 	}
713dbed73cbSSangeeta Misra 	if (rc == ILB_STATUS_OK) {
714dbed73cbSSangeeta Misra 		if (adt_put_event(event, ADT_SUCCESS, ADT_SUCCESS) != 0) {
715dbed73cbSSangeeta Misra 			logerr("ilbd_audit_rule_event:adt_put_event failed");
716dbed73cbSSangeeta Misra 			exit(EXIT_FAILURE);
717dbed73cbSSangeeta Misra 		}
718dbed73cbSSangeeta Misra 	} else {
719dbed73cbSSangeeta Misra 		audit_error = ilberror2auditerror(rc);
720dbed73cbSSangeeta Misra 		if (adt_put_event(event, ADT_FAILURE, audit_error) != 0) {
721dbed73cbSSangeeta Misra 			logerr("ilbd_audit_rule_event: adt_put_event failed");
722dbed73cbSSangeeta Misra 			exit(EXIT_FAILURE);
723dbed73cbSSangeeta Misra 		}
724dbed73cbSSangeeta Misra 	}
725dbed73cbSSangeeta Misra 	adt_free_event(event);
7263ae6a67dSSangeeta Misra 	free(aobuf);
7273ae6a67dSSangeeta Misra 	free(valstr1);
7283ae6a67dSSangeeta Misra 	free(valstr2);
729dbed73cbSSangeeta Misra 	(void) adt_end_session(ah);
730dbed73cbSSangeeta Misra }
7315df82708SSangeeta Misra /*
7325df82708SSangeeta Misra  * converts IP address from in6_addr format to uint32_t[4]
7335df82708SSangeeta Misra  * This conversion is needed for recording IP address in
7345df82708SSangeeta Misra  * audit records.
7355df82708SSangeeta Misra  */
7365df82708SSangeeta Misra void
cvt_addr(uint32_t * audit,int32_t type,struct in6_addr address)7375df82708SSangeeta Misra cvt_addr(uint32_t *audit, int32_t type, struct in6_addr address)
7385df82708SSangeeta Misra {
7395df82708SSangeeta Misra 
7405df82708SSangeeta Misra 	if (type == ADT_IPv4)  {
7415df82708SSangeeta Misra 		/* address is IPv4 */
7425df82708SSangeeta Misra 		audit[0] = address._S6_un._S6_u32[3];
7435df82708SSangeeta Misra 	} else {
7445df82708SSangeeta Misra 		/* address is IPv6 */
7455df82708SSangeeta Misra 		(void) memcpy(audit, address._S6_un._S6_u32,
7465df82708SSangeeta Misra 		    (4 * sizeof (uint32_t)));
7475df82708SSangeeta Misra 	}
7485df82708SSangeeta Misra }
749dbed73cbSSangeeta Misra 
750dbed73cbSSangeeta Misra static ilb_status_t
i_ilbd_action_switch(ilbd_rule_t * irl,ilbd_cmd_t cmd,boolean_t is_rollback,ucred_t * ucredp)751dbed73cbSSangeeta Misra i_ilbd_action_switch(ilbd_rule_t *irl, ilbd_cmd_t cmd,
752dbed73cbSSangeeta Misra     boolean_t is_rollback, ucred_t *ucredp)
753dbed73cbSSangeeta Misra {
754dbed73cbSSangeeta Misra 	ilb_status_t    rc;
755dbed73cbSSangeeta Misra 
756dbed73cbSSangeeta Misra 	switch (cmd) {
757dbed73cbSSangeeta Misra 	case ILBD_DESTROY_RULE:
758dbed73cbSSangeeta Misra 		rc = ilbd_destroy_one_rule(irl);
759dbed73cbSSangeeta Misra 		if (!is_rollback) {
760dbed73cbSSangeeta Misra 			ilbd_audit_rule_event(irl->irl_name, NULL,
761dbed73cbSSangeeta Misra 			    cmd, rc, ucredp);
762dbed73cbSSangeeta Misra 		}
763dbed73cbSSangeeta Misra 		return (rc);
764dbed73cbSSangeeta Misra 	case ILBD_ENABLE_RULE:
765dbed73cbSSangeeta Misra 		rc = ilbd_enable_one_rule(irl, is_rollback);
766dbed73cbSSangeeta Misra 		if (!is_rollback) {
767dbed73cbSSangeeta Misra 			ilbd_audit_rule_event(irl->irl_name, NULL, cmd,
768dbed73cbSSangeeta Misra 			    rc, ucredp);
769dbed73cbSSangeeta Misra 		}
770dbed73cbSSangeeta Misra 		return (rc);
771dbed73cbSSangeeta Misra 	case ILBD_DISABLE_RULE:
772dbed73cbSSangeeta Misra 		rc = ilbd_disable_one_rule(irl, is_rollback);
773dbed73cbSSangeeta Misra 		if (!is_rollback) {
774dbed73cbSSangeeta Misra 			ilbd_audit_rule_event(irl->irl_name, NULL, cmd,
775dbed73cbSSangeeta Misra 			    rc, ucredp);
776dbed73cbSSangeeta Misra 		}
777dbed73cbSSangeeta Misra 		return (rc);
778dbed73cbSSangeeta Misra 	}
779dbed73cbSSangeeta Misra 	return (ILB_STATUS_INVAL_CMD);
780dbed73cbSSangeeta Misra }
781dbed73cbSSangeeta Misra 
782dbed73cbSSangeeta Misra static ilb_cmd_t
i_ilbd2ilb_cmd(ilbd_cmd_t c)783dbed73cbSSangeeta Misra i_ilbd2ilb_cmd(ilbd_cmd_t c)
784dbed73cbSSangeeta Misra {
785dbed73cbSSangeeta Misra 	ilb_cmd_t	r;
786dbed73cbSSangeeta Misra 
787dbed73cbSSangeeta Misra 	switch (c) {
788dbed73cbSSangeeta Misra 	case ILBD_CREATE_RULE:
789dbed73cbSSangeeta Misra 		r = ILB_CREATE_RULE;
790dbed73cbSSangeeta Misra 		break;
791dbed73cbSSangeeta Misra 	case ILBD_DESTROY_RULE:
792dbed73cbSSangeeta Misra 		r = ILB_DESTROY_RULE;
793dbed73cbSSangeeta Misra 		break;
794dbed73cbSSangeeta Misra 	case ILBD_ENABLE_RULE:
795dbed73cbSSangeeta Misra 		r = ILB_ENABLE_RULE;
796dbed73cbSSangeeta Misra 		break;
797dbed73cbSSangeeta Misra 	case ILBD_DISABLE_RULE:
798dbed73cbSSangeeta Misra 		r = ILB_DISABLE_RULE;
799dbed73cbSSangeeta Misra 		break;
800dbed73cbSSangeeta Misra 	}
801dbed73cbSSangeeta Misra 	return (r);
802dbed73cbSSangeeta Misra }
803dbed73cbSSangeeta Misra 
804dbed73cbSSangeeta Misra static ilbd_cmd_t
get_undo_cmd(ilbd_cmd_t cmd)805dbed73cbSSangeeta Misra get_undo_cmd(ilbd_cmd_t cmd)
806dbed73cbSSangeeta Misra {
807dbed73cbSSangeeta Misra 	ilbd_cmd_t	u_cmd;
808dbed73cbSSangeeta Misra 
809dbed73cbSSangeeta Misra 	switch (cmd) {
810dbed73cbSSangeeta Misra 	case ILBD_DESTROY_RULE:
811dbed73cbSSangeeta Misra 		u_cmd = ILBD_BAD_CMD;
812dbed73cbSSangeeta Misra 		break;
813dbed73cbSSangeeta Misra 	case ILBD_ENABLE_RULE:
814dbed73cbSSangeeta Misra 		u_cmd = ILBD_DISABLE_RULE;
815dbed73cbSSangeeta Misra 		break;
816dbed73cbSSangeeta Misra 	case ILBD_DISABLE_RULE:
817dbed73cbSSangeeta Misra 		u_cmd = ILBD_ENABLE_RULE;
818dbed73cbSSangeeta Misra 		break;
819dbed73cbSSangeeta Misra 	}
820dbed73cbSSangeeta Misra 
821dbed73cbSSangeeta Misra 	return (u_cmd);
822dbed73cbSSangeeta Misra }
823dbed73cbSSangeeta Misra 
824dbed73cbSSangeeta Misra static ilb_status_t
i_ilbd_rule_action(const char * rule_name,const struct passwd * ps,ilbd_cmd_t cmd,ucred_t * ucredp)825dbed73cbSSangeeta Misra i_ilbd_rule_action(const char *rule_name, const struct passwd *ps,
826dbed73cbSSangeeta Misra     ilbd_cmd_t cmd, ucred_t *ucredp)
827dbed73cbSSangeeta Misra {
828dbed73cbSSangeeta Misra 	ilbd_rule_t	*irl, *irl_next;
829dbed73cbSSangeeta Misra 	boolean_t	is_all_rules = B_FALSE;
830dbed73cbSSangeeta Misra 	ilb_status_t	rc = ILB_STATUS_OK;
831dbed73cbSSangeeta Misra 	ilb_name_cmd_t	kcmd;
832dbed73cbSSangeeta Misra 	ilbd_cmd_t	u_cmd;
833dbed73cbSSangeeta Misra 	char    rulename[ILB_NAMESZ];
834dbed73cbSSangeeta Misra 
835dbed73cbSSangeeta Misra 	if (ps != NULL) {
836