xref: /onnv-gate/usr/src/lib/libipmp/common/ipmp_query.c (revision 8485:633e5b5eb268)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8485SPeter.Memishian@Sun.COM  * Common Development and Distribution License (the "License").
6*8485SPeter.Memishian@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
20*8485SPeter.Memishian@Sun.COM  *
21*8485SPeter.Memishian@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
220Sstevel@tonic-gate  * Use is subject to license terms.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
26*8485SPeter.Memishian@Sun.COM  * IPMP query interfaces (see PSARC/2002/615 and PSARC/2007/272).
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <assert.h>
300Sstevel@tonic-gate #include <errno.h>
31*8485SPeter.Memishian@Sun.COM #include <libinetutil.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <sys/types.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include "ipmp_impl.h"
380Sstevel@tonic-gate #include "ipmp_mpathd.h"
390Sstevel@tonic-gate #include "ipmp_query_impl.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate static ipmp_ifinfo_t	*ipmp_ifinfo_clone(ipmp_ifinfo_t *);
42*8485SPeter.Memishian@Sun.COM static ipmp_addrinfo_t	*ipmp_addrinfo_clone(ipmp_addrinfo_t *);
43*8485SPeter.Memishian@Sun.COM static ipmp_addrlist_t	*ipmp_addrlist_clone(ipmp_addrlist_t *);
440Sstevel@tonic-gate static ipmp_grouplist_t	*ipmp_grouplist_clone(ipmp_grouplist_t *);
450Sstevel@tonic-gate static ipmp_groupinfo_t	*ipmp_groupinfo_clone(ipmp_groupinfo_t *);
46*8485SPeter.Memishian@Sun.COM static ipmp_iflist_t	*ipmp_iflist_create(uint_t, char (*)[LIFNAMSIZ]);
47*8485SPeter.Memishian@Sun.COM static void		ipmp_freeiflist(ipmp_iflist_t *);
48*8485SPeter.Memishian@Sun.COM static ipmp_addrlist_t *ipmp_addrlist_create(uint_t, struct sockaddr_storage *);
49*8485SPeter.Memishian@Sun.COM static void		ipmp_freeaddrlist(ipmp_addrlist_t *);
500Sstevel@tonic-gate static ipmp_groupinfo_t *ipmp_snap_getgroupinfo(ipmp_snap_t *, const char *);
510Sstevel@tonic-gate static ipmp_ifinfo_t	*ipmp_snap_getifinfo(ipmp_snap_t *, const char *);
52*8485SPeter.Memishian@Sun.COM static ipmp_addrinfo_t  *ipmp_snap_getaddrinfo(ipmp_snap_t *, const char *,
53*8485SPeter.Memishian@Sun.COM 			    struct sockaddr_storage *);
540Sstevel@tonic-gate static int		ipmp_snap_take(ipmp_state_t *, ipmp_snap_t **);
550Sstevel@tonic-gate static boolean_t	ipmp_checktlv(ipmp_infotype_t, size_t, void *);
560Sstevel@tonic-gate static int		ipmp_querydone(ipmp_state_t *, int);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * Using `statep', send a query request for `type' to in.mpathd, and if
600Sstevel@tonic-gate  * necessary wait until at least `endtp' for a response.  Returns an IPMP
610Sstevel@tonic-gate  * error code.  If successful, the caller may then read additional query
620Sstevel@tonic-gate  * information through ipmp_readinfo(), and must eventually call
630Sstevel@tonic-gate  * ipmp_querydone() to complete the query operation.  Only one query may be
640Sstevel@tonic-gate  * outstanding on a given `statep' at a time.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate static int
ipmp_sendquery(ipmp_state_t * statep,ipmp_infotype_t type,const char * name,const void * addr,struct timeval * endtp)670Sstevel@tonic-gate ipmp_sendquery(ipmp_state_t *statep, ipmp_infotype_t type, const char *name,
68*8485SPeter.Memishian@Sun.COM     const void *addr, struct timeval *endtp)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	mi_query_t	query;
710Sstevel@tonic-gate 	mi_result_t	result;
720Sstevel@tonic-gate 	int		retval;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	query.miq_command = MI_QUERY;
750Sstevel@tonic-gate 	query.miq_inforeq = type;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	switch (type) {
78*8485SPeter.Memishian@Sun.COM 	case IPMP_ADDRINFO:
79*8485SPeter.Memishian@Sun.COM 		(void) strlcpy(query.miq_grname, name, LIFGRNAMSIZ);
80*8485SPeter.Memishian@Sun.COM 		query.miq_addr = *(struct sockaddr_storage *)addr;
81*8485SPeter.Memishian@Sun.COM 		break;
82*8485SPeter.Memishian@Sun.COM 
830Sstevel@tonic-gate 	case IPMP_GROUPINFO:
840Sstevel@tonic-gate 		(void) strlcpy(query.miq_grname, name, LIFGRNAMSIZ);
850Sstevel@tonic-gate 		break;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	case IPMP_IFINFO:
880Sstevel@tonic-gate 		(void) strlcpy(query.miq_ifname, name, LIFNAMSIZ);
890Sstevel@tonic-gate 		break;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	case IPMP_GROUPLIST:
920Sstevel@tonic-gate 	case IPMP_SNAP:
930Sstevel@tonic-gate 		break;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	default:
960Sstevel@tonic-gate 		assert(0);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if (gettimeofday(endtp, NULL) == -1)
1000Sstevel@tonic-gate 		return (IPMP_FAILURE);
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	endtp->tv_sec += IPMP_REQTIMEOUT;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	assert(statep->st_fd == -1);
1050Sstevel@tonic-gate 	retval = ipmp_connect(&statep->st_fd);
1060Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
1070Sstevel@tonic-gate 		return (retval);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	retval = ipmp_write(statep->st_fd, &query, sizeof (query));
1100Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
1110Sstevel@tonic-gate 		return (ipmp_querydone(statep, retval));
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	retval = ipmp_read(statep->st_fd, &result, sizeof (result), endtp);
1140Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
1150Sstevel@tonic-gate 		return (ipmp_querydone(statep, retval));
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (result.me_mpathd_error != IPMP_SUCCESS)
1180Sstevel@tonic-gate 		return (ipmp_querydone(statep, result.me_mpathd_error));
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	return (IPMP_SUCCESS);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * Using `statep', read a query response of type `infotype' into a dynamically
1250Sstevel@tonic-gate  * allocated buffer pointed to by `*infop', before the current time becomes
1260Sstevel@tonic-gate  * `endtp'.  Returns an IPMP error code.
1270Sstevel@tonic-gate  */
1280Sstevel@tonic-gate static int
ipmp_readinfo(ipmp_state_t * statep,ipmp_infotype_t infotype,void ** infop,const struct timeval * endtp)1290Sstevel@tonic-gate ipmp_readinfo(ipmp_state_t *statep, ipmp_infotype_t infotype, void **infop,
1300Sstevel@tonic-gate     const struct timeval *endtp)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	int		retval;
1330Sstevel@tonic-gate 	size_t		len;
1340Sstevel@tonic-gate 	ipmp_infotype_t	type;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	retval = ipmp_readtlv(statep->st_fd, &type, &len, infop, endtp);
1370Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
1380Sstevel@tonic-gate 		return (retval);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	if (type != infotype || !ipmp_checktlv(type, len, *infop)) {
1410Sstevel@tonic-gate 		free(*infop);
1420Sstevel@tonic-gate 		return (IPMP_EPROTO);
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	return (IPMP_SUCCESS);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
149*8485SPeter.Memishian@Sun.COM  * Using `statep', read in the remaining IPMP group information TLVs from
150*8485SPeter.Memishian@Sun.COM  * in.mpathd into `grinfop' before the current time becomes `endtp'.  Returns
151*8485SPeter.Memishian@Sun.COM  * an IPMP error code.  On failure, `grinfop' will have its original contents.
152*8485SPeter.Memishian@Sun.COM  */
153*8485SPeter.Memishian@Sun.COM static int
ipmp_readgroupinfo_lists(ipmp_state_t * statep,ipmp_groupinfo_t * grinfop,const struct timeval * endtp)154*8485SPeter.Memishian@Sun.COM ipmp_readgroupinfo_lists(ipmp_state_t *statep, ipmp_groupinfo_t *grinfop,
155*8485SPeter.Memishian@Sun.COM     const struct timeval *endtp)
156*8485SPeter.Memishian@Sun.COM {
157*8485SPeter.Memishian@Sun.COM 	int retval;
158*8485SPeter.Memishian@Sun.COM 	ipmp_iflist_t *iflistp;
159*8485SPeter.Memishian@Sun.COM 	ipmp_addrlist_t *adlistp;
160*8485SPeter.Memishian@Sun.COM 
161*8485SPeter.Memishian@Sun.COM 	retval = ipmp_readinfo(statep, IPMP_IFLIST, (void **)&iflistp, endtp);
162*8485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS)
163*8485SPeter.Memishian@Sun.COM 		return (retval);
164*8485SPeter.Memishian@Sun.COM 
165*8485SPeter.Memishian@Sun.COM 	retval = ipmp_readinfo(statep, IPMP_ADDRLIST, (void **)&adlistp, endtp);
166*8485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS) {
167*8485SPeter.Memishian@Sun.COM 		ipmp_freeiflist(iflistp);
168*8485SPeter.Memishian@Sun.COM 		return (retval);
169*8485SPeter.Memishian@Sun.COM 	}
170*8485SPeter.Memishian@Sun.COM 
171*8485SPeter.Memishian@Sun.COM 	grinfop->gr_iflistp = iflistp;
172*8485SPeter.Memishian@Sun.COM 	grinfop->gr_adlistp = adlistp;
173*8485SPeter.Memishian@Sun.COM 	return (IPMP_SUCCESS);
174*8485SPeter.Memishian@Sun.COM }
175*8485SPeter.Memishian@Sun.COM 
176*8485SPeter.Memishian@Sun.COM /*
177*8485SPeter.Memishian@Sun.COM  * Using `statep', read in the remaining IPMP interface information TLVs from
178*8485SPeter.Memishian@Sun.COM  * in.mpathd into `ifinfop' before the current time becomes `endtp'.  Returns
179*8485SPeter.Memishian@Sun.COM  * an IPMP error code.  On failure, `ifinfop' will have its original contents.
180*8485SPeter.Memishian@Sun.COM  */
181*8485SPeter.Memishian@Sun.COM static int
ipmp_readifinfo_lists(ipmp_state_t * statep,ipmp_ifinfo_t * ifinfop,const struct timeval * endtp)182*8485SPeter.Memishian@Sun.COM ipmp_readifinfo_lists(ipmp_state_t *statep, ipmp_ifinfo_t *ifinfop,
183*8485SPeter.Memishian@Sun.COM     const struct timeval *endtp)
184*8485SPeter.Memishian@Sun.COM {
185*8485SPeter.Memishian@Sun.COM 	int retval;
186*8485SPeter.Memishian@Sun.COM 	ipmp_addrlist_t *tlist4p, *tlist6p;
187*8485SPeter.Memishian@Sun.COM 
188*8485SPeter.Memishian@Sun.COM 	retval = ipmp_readinfo(statep, IPMP_ADDRLIST, (void **)&tlist4p, endtp);
189*8485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS)
190*8485SPeter.Memishian@Sun.COM 		return (retval);
191*8485SPeter.Memishian@Sun.COM 
192*8485SPeter.Memishian@Sun.COM 	retval = ipmp_readinfo(statep, IPMP_ADDRLIST, (void **)&tlist6p, endtp);
193*8485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS) {
194*8485SPeter.Memishian@Sun.COM 		ipmp_freeaddrlist(tlist4p);
195*8485SPeter.Memishian@Sun.COM 		return (retval);
196*8485SPeter.Memishian@Sun.COM 	}
197*8485SPeter.Memishian@Sun.COM 
198*8485SPeter.Memishian@Sun.COM 	ifinfop->if_targinfo4.it_targlistp = tlist4p;
199*8485SPeter.Memishian@Sun.COM 	ifinfop->if_targinfo6.it_targlistp = tlist6p;
200*8485SPeter.Memishian@Sun.COM 	return (IPMP_SUCCESS);
201*8485SPeter.Memishian@Sun.COM }
202*8485SPeter.Memishian@Sun.COM 
203*8485SPeter.Memishian@Sun.COM /*
2040Sstevel@tonic-gate  * Complete the query operation started in ipmp_sendquery().  The interface is
2050Sstevel@tonic-gate  * designed to be easy to use in the `return' statement of a function, and
2060Sstevel@tonic-gate  * thus returns the passed in `retval' and preserves `errno'.
2070Sstevel@tonic-gate  */
2080Sstevel@tonic-gate static int
ipmp_querydone(ipmp_state_t * statep,int retval)2090Sstevel@tonic-gate ipmp_querydone(ipmp_state_t *statep, int retval)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	int error = errno;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	(void) close(statep->st_fd);
2140Sstevel@tonic-gate 	statep->st_fd = -1;
2150Sstevel@tonic-gate 	errno = error;
2160Sstevel@tonic-gate 	return (retval);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * Using `handle', get the group list and store the results in a dynamically
2210Sstevel@tonic-gate  * allocated buffer pointed to by `*grlistpp'.  Returns an IPMP error code.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate int
ipmp_getgrouplist(ipmp_handle_t handle,ipmp_grouplist_t ** grlistpp)2240Sstevel@tonic-gate ipmp_getgrouplist(ipmp_handle_t handle, ipmp_grouplist_t **grlistpp)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	ipmp_state_t	*statep = handle;
2270Sstevel@tonic-gate 	struct timeval	end;
2280Sstevel@tonic-gate 	int		retval;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if (statep->st_snap != NULL) {
2310Sstevel@tonic-gate 		*grlistpp = ipmp_grouplist_clone(statep->st_snap->sn_grlistp);
2320Sstevel@tonic-gate 		return (*grlistpp != NULL ? IPMP_SUCCESS : IPMP_ENOMEM);
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
235*8485SPeter.Memishian@Sun.COM 	retval = ipmp_sendquery(statep, IPMP_GROUPLIST, NULL, NULL, &end);
2360Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
2370Sstevel@tonic-gate 		return (retval);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	retval = ipmp_readinfo(statep, IPMP_GROUPLIST, (void **)grlistpp, &end);
2400Sstevel@tonic-gate 	return (ipmp_querydone(statep, retval));
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate /*
2440Sstevel@tonic-gate  * Free the group list pointed to by `grlistp'.
2450Sstevel@tonic-gate  */
2460Sstevel@tonic-gate void
ipmp_freegrouplist(ipmp_grouplist_t * grlistp)2470Sstevel@tonic-gate ipmp_freegrouplist(ipmp_grouplist_t *grlistp)
2480Sstevel@tonic-gate {
2490Sstevel@tonic-gate 	free(grlistp);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * Using `handle', get the group information associated with group `name' and
2540Sstevel@tonic-gate  * store the results in a dynamically allocated buffer pointed to by
2550Sstevel@tonic-gate  * `*grinfopp'.  Returns an IPMP error code.
2560Sstevel@tonic-gate  */
2570Sstevel@tonic-gate int
ipmp_getgroupinfo(ipmp_handle_t handle,const char * name,ipmp_groupinfo_t ** grinfopp)2580Sstevel@tonic-gate ipmp_getgroupinfo(ipmp_handle_t handle, const char *name,
2590Sstevel@tonic-gate     ipmp_groupinfo_t **grinfopp)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	ipmp_state_t	*statep = handle;
2620Sstevel@tonic-gate 	int		retval;
2630Sstevel@tonic-gate 	struct timeval	end;
2640Sstevel@tonic-gate 	ipmp_groupinfo_t *grinfop;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (statep->st_snap != NULL) {
2670Sstevel@tonic-gate 		grinfop = ipmp_snap_getgroupinfo(statep->st_snap, name);
2680Sstevel@tonic-gate 		if (grinfop == NULL)
2690Sstevel@tonic-gate 			return (IPMP_EUNKGROUP);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 		*grinfopp = ipmp_groupinfo_clone(grinfop);
2720Sstevel@tonic-gate 		return (*grinfopp != NULL ? IPMP_SUCCESS : IPMP_ENOMEM);
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 
275*8485SPeter.Memishian@Sun.COM 	retval = ipmp_sendquery(statep, IPMP_GROUPINFO, name, NULL, &end);
2760Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
2770Sstevel@tonic-gate 		return (retval);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	retval = ipmp_readinfo(statep, IPMP_GROUPINFO, (void **)grinfopp, &end);
2800Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
2810Sstevel@tonic-gate 		return (ipmp_querydone(statep, retval));
2820Sstevel@tonic-gate 
283*8485SPeter.Memishian@Sun.COM 	retval = ipmp_readgroupinfo_lists(statep, *grinfopp, &end);
2840Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
2850Sstevel@tonic-gate 		free(*grinfopp);
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	return (ipmp_querydone(statep, retval));
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * Free the group information pointed to by `grinfop'.
2920Sstevel@tonic-gate  */
2930Sstevel@tonic-gate void
ipmp_freegroupinfo(ipmp_groupinfo_t * grinfop)2940Sstevel@tonic-gate ipmp_freegroupinfo(ipmp_groupinfo_t *grinfop)
2950Sstevel@tonic-gate {
296*8485SPeter.Memishian@Sun.COM 	ipmp_freeaddrlist(grinfop->gr_adlistp);
297*8485SPeter.Memishian@Sun.COM 	ipmp_freeiflist(grinfop->gr_iflistp);
2980Sstevel@tonic-gate 	free(grinfop);
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate  * Using `handle', get the interface information associated with interface
3030Sstevel@tonic-gate  * `name' and store the results in a dynamically allocated buffer pointed to
3040Sstevel@tonic-gate  * by `*ifinfopp'.  Returns an IPMP error code.
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate int
ipmp_getifinfo(ipmp_handle_t handle,const char * name,ipmp_ifinfo_t ** ifinfopp)3070Sstevel@tonic-gate ipmp_getifinfo(ipmp_handle_t handle, const char *name, ipmp_ifinfo_t **ifinfopp)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	ipmp_state_t	*statep = handle;
3100Sstevel@tonic-gate 	ipmp_ifinfo_t	*ifinfop;
3110Sstevel@tonic-gate 	int		retval;
3120Sstevel@tonic-gate 	struct timeval	end;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	if (statep->st_snap != NULL) {
3150Sstevel@tonic-gate 		ifinfop = ipmp_snap_getifinfo(statep->st_snap, name);
3160Sstevel@tonic-gate 		if (ifinfop == NULL)
3170Sstevel@tonic-gate 			return (IPMP_EUNKIF);
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 		*ifinfopp = ipmp_ifinfo_clone(ifinfop);
3200Sstevel@tonic-gate 		return (*ifinfopp != NULL ? IPMP_SUCCESS : IPMP_ENOMEM);
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
323*8485SPeter.Memishian@Sun.COM 	retval = ipmp_sendquery(statep, IPMP_IFINFO, name, NULL, &end);
3240Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS)
3250Sstevel@tonic-gate 		return (retval);
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	retval = ipmp_readinfo(statep, IPMP_IFINFO, (void **)ifinfopp, &end);
328*8485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS)
329*8485SPeter.Memishian@Sun.COM 		return (ipmp_querydone(statep, retval));
330*8485SPeter.Memishian@Sun.COM 
331*8485SPeter.Memishian@Sun.COM 	retval = ipmp_readifinfo_lists(statep, *ifinfopp, &end);
332*8485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS)
333*8485SPeter.Memishian@Sun.COM 		free(*ifinfopp);
334*8485SPeter.Memishian@Sun.COM 
3350Sstevel@tonic-gate 	return (ipmp_querydone(statep, retval));
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate  * Free the interface information pointed to by `ifinfop'.
3400Sstevel@tonic-gate  */
3410Sstevel@tonic-gate void
ipmp_freeifinfo(ipmp_ifinfo_t * ifinfop)3420Sstevel@tonic-gate ipmp_freeifinfo(ipmp_ifinfo_t *ifinfop)
3430Sstevel@tonic-gate {
344*8485SPeter.Memishian@Sun.COM 	ipmp_freeaddrlist(ifinfop->if_targinfo4.it_targlistp);
345*8485SPeter.Memishian@Sun.COM 	ipmp_freeaddrlist(ifinfop->if_targinfo6.it_targlistp);
3460Sstevel@tonic-gate 	free(ifinfop);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /*
350*8485SPeter.Memishian@Sun.COM  * Using `handle', get the address information associated with address `addrp'
351*8485SPeter.Memishian@Sun.COM  * on group `grname' and store the results in a dynamically allocated buffer
352*8485SPeter.Memishian@Sun.COM  * pointed to by `*adinfopp'.  Returns an IPMP error code.
353*8485SPeter.Memishian@Sun.COM  */
354*8485SPeter.Memishian@Sun.COM int
ipmp_getaddrinfo(ipmp_handle_t handle,const char * grname,struct sockaddr_storage * addrp,ipmp_addrinfo_t ** adinfopp)355*8485SPeter.Memishian@Sun.COM ipmp_getaddrinfo(ipmp_handle_t handle, const char *grname,
356*8485SPeter.Memishian@Sun.COM     struct sockaddr_storage *addrp, ipmp_addrinfo_t **adinfopp)
357*8485SPeter.Memishian@Sun.COM {
358*8485SPeter.Memishian@Sun.COM 	ipmp_state_t	*statep = handle;
359*8485SPeter.Memishian@Sun.COM 	ipmp_addrinfo_t	*adinfop;
360*8485SPeter.Memishian@Sun.COM 	int		retval;
361*8485SPeter.Memishian@Sun.COM 	struct timeval	end;
362*8485SPeter.Memishian@Sun.COM 
363*8485SPeter.Memishian@Sun.COM 	if (statep->st_snap != NULL) {
364*8485SPeter.Memishian@Sun.COM 		adinfop = ipmp_snap_getaddrinfo(statep->st_snap, grname, addrp);
365*8485SPeter.Memishian@Sun.COM 		if (adinfop == NULL)
366*8485SPeter.Memishian@Sun.COM 			return (IPMP_EUNKADDR);
367*8485SPeter.Memishian@Sun.COM 
368*8485SPeter.Memishian@Sun.COM 		*adinfopp = ipmp_addrinfo_clone(adinfop);
369*8485SPeter.Memishian@Sun.COM 		return (*adinfopp != NULL ? IPMP_SUCCESS : IPMP_ENOMEM);
370*8485SPeter.Memishian@Sun.COM 	}
371*8485SPeter.Memishian@Sun.COM 
372*8485SPeter.Memishian@Sun.COM 	retval = ipmp_sendquery(statep, IPMP_ADDRINFO, grname, addrp, &end);
373*8485SPeter.Memishian@Sun.COM 	if (retval != IPMP_SUCCESS)
374*8485SPeter.Memishian@Sun.COM 		return (retval);
375*8485SPeter.Memishian@Sun.COM 
376*8485SPeter.Memishian@Sun.COM 	retval = ipmp_readinfo(statep, IPMP_ADDRINFO, (void **)adinfopp, &end);
377*8485SPeter.Memishian@Sun.COM 	return (ipmp_querydone(statep, retval));
378*8485SPeter.Memishian@Sun.COM }
379*8485SPeter.Memishian@Sun.COM 
380*8485SPeter.Memishian@Sun.COM /*
381*8485SPeter.Memishian@Sun.COM  * Free the address information pointed to by `adinfop'.
382*8485SPeter.Memishian@Sun.COM  */
383*8485SPeter.Memishian@Sun.COM void
ipmp_freeaddrinfo(ipmp_addrinfo_t * adinfop)384*8485SPeter.Memishian@Sun.COM ipmp_freeaddrinfo(ipmp_addrinfo_t *adinfop)
385*8485SPeter.Memishian@Sun.COM {
386*8485SPeter.Memishian@Sun.COM 	free(adinfop);
387*8485SPeter.Memishian@Sun.COM }
388*8485SPeter.Memishian@Sun.COM 
389*8485SPeter.Memishian@Sun.COM /*
3900Sstevel@tonic-gate  * Check if `buf' has a NUL byte in its first `bufsize' bytes.
3910Sstevel@tonic-gate  */
3920Sstevel@tonic-gate static boolean_t
hasnulbyte(const char * buf,size_t bufsize)3930Sstevel@tonic-gate hasnulbyte(const char *buf, size_t bufsize)
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	while (bufsize-- > 0) {
3960Sstevel@tonic-gate 		if (buf[bufsize] == '\0')
3970Sstevel@tonic-gate 			return (B_TRUE);
3980Sstevel@tonic-gate 	}
3990Sstevel@tonic-gate 	return (B_FALSE);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate /*
4030Sstevel@tonic-gate  * Check that the TLV triplet named by `type', `len' and `value' is correctly
4040Sstevel@tonic-gate  * formed.
4050Sstevel@tonic-gate  */
4060Sstevel@tonic-gate static boolean_t
ipmp_checktlv(ipmp_infotype_t type,size_t len,void * value)4070Sstevel@tonic-gate ipmp_checktlv(ipmp_infotype_t type, size_t len, void *value)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate 	ipmp_iflist_t		*iflistp;
4100Sstevel@tonic-gate 	ipmp_ifinfo_t		*ifinfop;
4110Sstevel@tonic-gate 	ipmp_grouplist_t	*grlistp;
4120Sstevel@tonic-gate 	ipmp_groupinfo_t	*grinfop;
413*8485SPeter.Memishian@Sun.COM 	ipmp_addrlist_t		*adlistp;
4140Sstevel@tonic-gate 	unsigned int		i;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate 	switch (type) {
417*8485SPeter.Memishian@Sun.COM 	case IPMP_ADDRINFO:
418*8485SPeter.Memishian@Sun.COM 		if (len != sizeof (ipmp_addrinfo_t))
419*8485SPeter.Memishian@Sun.COM 			return (B_FALSE);
420*8485SPeter.Memishian@Sun.COM 		break;
421*8485SPeter.Memishian@Sun.COM 
422*8485SPeter.Memishian@Sun.COM 	case IPMP_ADDRLIST:
423*8485SPeter.Memishian@Sun.COM 		adlistp = (ipmp_addrlist_t *)value;
424*8485SPeter.Memishian@Sun.COM 		if (len < IPMP_ADDRLIST_SIZE(0) ||
425*8485SPeter.Memishian@Sun.COM 		    len < IPMP_ADDRLIST_SIZE(adlistp->al_naddr))
426*8485SPeter.Memishian@Sun.COM 			return (B_FALSE);
427*8485SPeter.Memishian@Sun.COM 		break;
428*8485SPeter.Memishian@Sun.COM 
4290Sstevel@tonic-gate 	case IPMP_IFLIST:
4300Sstevel@tonic-gate 		iflistp = (ipmp_iflist_t *)value;
431*8485SPeter.Memishian@Sun.COM 		if (len < IPMP_IFLIST_SIZE(0) ||
4320Sstevel@tonic-gate 		    len < IPMP_IFLIST_SIZE(iflistp->il_nif))
4330Sstevel@tonic-gate 			return (B_FALSE);
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 		for (i = 0; i < iflistp->il_nif; i++)
4360Sstevel@tonic-gate 			if (!hasnulbyte(iflistp->il_ifs[i], LIFNAMSIZ))
4370Sstevel@tonic-gate 				return (B_FALSE);
4380Sstevel@tonic-gate 		break;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	case IPMP_IFINFO:
4410Sstevel@tonic-gate 		ifinfop = (ipmp_ifinfo_t *)value;
4420Sstevel@tonic-gate 		if (len != sizeof (ipmp_ifinfo_t))
4430Sstevel@tonic-gate 			return (B_FALSE);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 		if (!hasnulbyte(ifinfop->if_name, LIFNAMSIZ) ||
4460Sstevel@tonic-gate 		    !hasnulbyte(ifinfop->if_group, LIFGRNAMSIZ))
4470Sstevel@tonic-gate 			return (B_FALSE);
4480Sstevel@tonic-gate 		break;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	case IPMP_GROUPLIST:
4510Sstevel@tonic-gate 		grlistp = (ipmp_grouplist_t *)value;
452*8485SPeter.Memishian@Sun.COM 		if (len < IPMP_GROUPLIST_SIZE(0) ||
4530Sstevel@tonic-gate 		    len < IPMP_GROUPLIST_SIZE(grlistp->gl_ngroup))
4540Sstevel@tonic-gate 			return (B_FALSE);
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 		for (i = 0; i < grlistp->gl_ngroup; i++)
4570Sstevel@tonic-gate 			if (!hasnulbyte(grlistp->gl_groups[i], LIFGRNAMSIZ))
4580Sstevel@tonic-gate 				return (B_FALSE);
4590Sstevel@tonic-gate 		break;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	case IPMP_GROUPINFO:
4620Sstevel@tonic-gate 		grinfop = (ipmp_groupinfo_t *)value;
4630Sstevel@tonic-gate 		if (len != sizeof (ipmp_groupinfo_t))
4640Sstevel@tonic-gate 			return (B_FALSE);
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		if (!hasnulbyte(grinfop->gr_name, LIFGRNAMSIZ))
4670Sstevel@tonic-gate 			return (B_FALSE);
4680Sstevel@tonic-gate 		break;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	case IPMP_SNAP:
4710Sstevel@tonic-gate 		if (len != sizeof (ipmp_snap_t))
4720Sstevel@tonic-gate 			return (B_FALSE);
4730Sstevel@tonic-gate 		break;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	default:
4760Sstevel@tonic-gate 		return (B_FALSE);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	return (B_TRUE);
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate /*
483*8485SPeter.Memishian@Sun.COM  * Create a group list; arguments match ipmp_grouplist_t fields.  Returns a
484*8485SPeter.Memishian@Sun.COM  * pointer to the new group list on success, or NULL on failure.
4850Sstevel@tonic-gate  */
4860Sstevel@tonic-gate ipmp_grouplist_t *
ipmp_grouplist_create(uint64_t sig,unsigned int ngroup,char (* groups)[LIFGRNAMSIZ])4870Sstevel@tonic-gate ipmp_grouplist_create(uint64_t sig, unsigned int ngroup,
4880Sstevel@tonic-gate     char (*groups)[LIFGRNAMSIZ])
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate 	unsigned int i;
4910Sstevel@tonic-gate 	ipmp_grouplist_t *grlistp;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	grlistp = malloc(IPMP_GROUPLIST_SIZE(ngroup));
4940Sstevel@tonic-gate 	if (grlistp == NULL)
4950Sstevel@tonic-gate 		return (NULL);
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 	grlistp->gl_sig = sig;
4980Sstevel@tonic-gate 	grlistp->gl_ngroup = ngroup;
4990Sstevel@tonic-gate 	for (i = 0; i < ngroup; i++)
5000Sstevel@tonic-gate 		(void) strlcpy(grlistp->gl_groups[i], groups[i], LIFGRNAMSIZ);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	return (grlistp);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate /*
5060Sstevel@tonic-gate  * Clone the group list named by `grlistp'.  Returns a pointer to the clone on
5070Sstevel@tonic-gate  * success, or NULL on failure.
5080Sstevel@tonic-gate  */
5090Sstevel@tonic-gate ipmp_grouplist_t *
ipmp_grouplist_clone(ipmp_grouplist_t * grlistp)5100Sstevel@tonic-gate ipmp_grouplist_clone(ipmp_grouplist_t *grlistp)
5110Sstevel@tonic-gate {
5120Sstevel@tonic-gate 	return (ipmp_grouplist_create(grlistp->gl_sig, grlistp->gl_ngroup,
5130Sstevel@tonic-gate 	    grlistp->gl_groups));
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate /*
517*8485SPeter.Memishian@Sun.COM  * Create target information; arguments match ipmp_targinfo_t fields.  Returns
518*8485SPeter.Memishian@Sun.COM  * a pointer to the new target info on success, or NULL on failure.
519*8485SPeter.Memishian@Sun.COM  */
520*8485SPeter.Memishian@Sun.COM ipmp_targinfo_t *
ipmp_targinfo_create(const char * name,struct sockaddr_storage * testaddrp,ipmp_if_targmode_t targmode,uint_t ntarg,struct sockaddr_storage * targs)521*8485SPeter.Memishian@Sun.COM ipmp_targinfo_create(const char *name, struct sockaddr_storage *testaddrp,
522*8485SPeter.Memishian@Sun.COM     ipmp_if_targmode_t targmode, uint_t ntarg, struct sockaddr_storage *targs)
523*8485SPeter.Memishian@Sun.COM {
524*8485SPeter.Memishian@Sun.COM 	ipmp_targinfo_t *targinfop;
525*8485SPeter.Memishian@Sun.COM 
526*8485SPeter.Memishian@Sun.COM 	targinfop = malloc(sizeof (ipmp_targinfo_t));
527*8485SPeter.Memishian@Sun.COM 	if (targinfop == NULL)
528*8485SPeter.Memishian@Sun.COM 		return (NULL);
529*8485SPeter.Memishian@Sun.COM 
530*8485SPeter.Memishian@Sun.COM 	targinfop->it_testaddr = *testaddrp;
531*8485SPeter.Memishian@Sun.COM 	targinfop->it_targmode = targmode;
532*8485SPeter.Memishian@Sun.COM 	targinfop->it_targlistp = ipmp_addrlist_create(ntarg, targs);
533*8485SPeter.Memishian@Sun.COM 	if (targinfop->it_targlistp == NULL) {
534*8485SPeter.Memishian@Sun.COM 		ipmp_freetarginfo(targinfop);
535*8485SPeter.Memishian@Sun.COM 		return (NULL);
536*8485SPeter.Memishian@Sun.COM 	}
537*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(targinfop->it_name, name, LIFNAMSIZ);
538*8485SPeter.Memishian@Sun.COM 
539*8485SPeter.Memishian@Sun.COM 	return (targinfop);
540*8485SPeter.Memishian@Sun.COM }
541*8485SPeter.Memishian@Sun.COM 
542*8485SPeter.Memishian@Sun.COM /*
543*8485SPeter.Memishian@Sun.COM  * Free the target information pointed to by `targinfop'.
544*8485SPeter.Memishian@Sun.COM  */
545*8485SPeter.Memishian@Sun.COM void
ipmp_freetarginfo(ipmp_targinfo_t * targinfop)546*8485SPeter.Memishian@Sun.COM ipmp_freetarginfo(ipmp_targinfo_t *targinfop)
547*8485SPeter.Memishian@Sun.COM {
548*8485SPeter.Memishian@Sun.COM 	free(targinfop->it_targlistp);
549*8485SPeter.Memishian@Sun.COM 	free(targinfop);
550*8485SPeter.Memishian@Sun.COM }
551*8485SPeter.Memishian@Sun.COM 
552*8485SPeter.Memishian@Sun.COM /*
553*8485SPeter.Memishian@Sun.COM  * Create an interface list; arguments match ipmp_iflist_t fields.  Returns a
554*8485SPeter.Memishian@Sun.COM  * pointer to the new interface list on success, or NULL on failure.
555*8485SPeter.Memishian@Sun.COM  */
556*8485SPeter.Memishian@Sun.COM static ipmp_iflist_t *
ipmp_iflist_create(uint_t nif,char (* ifs)[LIFNAMSIZ])557*8485SPeter.Memishian@Sun.COM ipmp_iflist_create(uint_t nif, char (*ifs)[LIFNAMSIZ])
558*8485SPeter.Memishian@Sun.COM {
559*8485SPeter.Memishian@Sun.COM 	unsigned int i;
560*8485SPeter.Memishian@Sun.COM 	ipmp_iflist_t *iflistp;
561*8485SPeter.Memishian@Sun.COM 
562*8485SPeter.Memishian@Sun.COM 	iflistp = malloc(IPMP_IFLIST_SIZE(nif));
563*8485SPeter.Memishian@Sun.COM 	if (iflistp == NULL)
564*8485SPeter.Memishian@Sun.COM 		return (NULL);
565*8485SPeter.Memishian@Sun.COM 
566*8485SPeter.Memishian@Sun.COM 	iflistp->il_nif = nif;
567*8485SPeter.Memishian@Sun.COM 	for (i = 0; i < nif; i++)
568*8485SPeter.Memishian@Sun.COM 		(void) strlcpy(iflistp->il_ifs[i], ifs[i], LIFNAMSIZ);
569*8485SPeter.Memishian@Sun.COM 
570*8485SPeter.Memishian@Sun.COM 	return (iflistp);
571*8485SPeter.Memishian@Sun.COM }
572*8485SPeter.Memishian@Sun.COM 
573*8485SPeter.Memishian@Sun.COM /*
574*8485SPeter.Memishian@Sun.COM  * Free the interface list pointed to by `iflistp'.
575*8485SPeter.Memishian@Sun.COM  */
576*8485SPeter.Memishian@Sun.COM static void
ipmp_freeiflist(ipmp_iflist_t * iflistp)577*8485SPeter.Memishian@Sun.COM ipmp_freeiflist(ipmp_iflist_t *iflistp)
578*8485SPeter.Memishian@Sun.COM {
579*8485SPeter.Memishian@Sun.COM 	free(iflistp);
580*8485SPeter.Memishian@Sun.COM }
581*8485SPeter.Memishian@Sun.COM 
582*8485SPeter.Memishian@Sun.COM /*
583*8485SPeter.Memishian@Sun.COM  * Create an interface; arguments match ipmp_ifinfo_t fields.  Returns a
584*8485SPeter.Memishian@Sun.COM  * pointer to the new interface on success, or NULL on failure.
5850Sstevel@tonic-gate  */
5860Sstevel@tonic-gate ipmp_ifinfo_t *
ipmp_ifinfo_create(const char * name,const char * group,ipmp_if_state_t state,ipmp_if_type_t type,ipmp_if_linkstate_t linkstate,ipmp_if_probestate_t probestate,ipmp_if_flags_t flags,ipmp_targinfo_t * targinfo4p,ipmp_targinfo_t * targinfo6p)5870Sstevel@tonic-gate ipmp_ifinfo_create(const char *name, const char *group, ipmp_if_state_t state,
588*8485SPeter.Memishian@Sun.COM     ipmp_if_type_t type, ipmp_if_linkstate_t linkstate,
589*8485SPeter.Memishian@Sun.COM     ipmp_if_probestate_t probestate, ipmp_if_flags_t flags,
590*8485SPeter.Memishian@Sun.COM     ipmp_targinfo_t *targinfo4p, ipmp_targinfo_t *targinfo6p)
5910Sstevel@tonic-gate {
5920Sstevel@tonic-gate 	ipmp_ifinfo_t *ifinfop;
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	ifinfop = malloc(sizeof (ipmp_ifinfo_t));
5950Sstevel@tonic-gate 	if (ifinfop == NULL)
5960Sstevel@tonic-gate 		return (NULL);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	(void) strlcpy(ifinfop->if_name, name, LIFNAMSIZ);
5990Sstevel@tonic-gate 	(void) strlcpy(ifinfop->if_group, group, LIFGRNAMSIZ);
600*8485SPeter.Memishian@Sun.COM 
601*8485SPeter.Memishian@Sun.COM 	ifinfop->if_state	= state;
602*8485SPeter.Memishian@Sun.COM 	ifinfop->if_type	= type;
603*8485SPeter.Memishian@Sun.COM 	ifinfop->if_linkstate	= linkstate;
604*8485SPeter.Memishian@Sun.COM 	ifinfop->if_probestate	= probestate;
605*8485SPeter.Memishian@Sun.COM 	ifinfop->if_flags	= flags;
606*8485SPeter.Memishian@Sun.COM 	ifinfop->if_targinfo4	= *targinfo4p;
607*8485SPeter.Memishian@Sun.COM 	ifinfop->if_targinfo6	= *targinfo6p;
608*8485SPeter.Memishian@Sun.COM 
609*8485SPeter.Memishian@Sun.COM 	ifinfop->if_targinfo4.it_targlistp =
610*8485SPeter.Memishian@Sun.COM 	    ipmp_addrlist_clone(targinfo4p->it_targlistp);
611*8485SPeter.Memishian@Sun.COM 	ifinfop->if_targinfo6.it_targlistp =
612*8485SPeter.Memishian@Sun.COM 	    ipmp_addrlist_clone(targinfo6p->it_targlistp);
613*8485SPeter.Memishian@Sun.COM 
614*8485SPeter.Memishian@Sun.COM 	if (ifinfop->if_targinfo4.it_targlistp == NULL ||
615*8485SPeter.Memishian@Sun.COM 	    ifinfop->if_targinfo6.it_targlistp == NULL) {
616*8485SPeter.Memishian@Sun.COM 		ipmp_freeifinfo(ifinfop);
617*8485SPeter.Memishian@Sun.COM 		return (NULL);
618*8485SPeter.Memishian@Sun.COM 	}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	return (ifinfop);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate  * Clone the interface information named by `ifinfop'.  Returns a pointer to
6250Sstevel@tonic-gate  * the clone on success, or NULL on failure.
6260Sstevel@tonic-gate  */
6270Sstevel@tonic-gate ipmp_ifinfo_t *
ipmp_ifinfo_clone(ipmp_ifinfo_t * ifinfop)6280Sstevel@tonic-gate ipmp_ifinfo_clone(ipmp_ifinfo_t *ifinfop)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	return (ipmp_ifinfo_create(ifinfop->if_name, ifinfop->if_group,
631*8485SPeter.Memishian@Sun.COM 	    ifinfop->if_state, ifinfop->if_type, ifinfop->if_linkstate,
632*8485SPeter.Memishian@Sun.COM 	    ifinfop->if_probestate, ifinfop->if_flags, &ifinfop->if_targinfo4,
633*8485SPeter.Memishian@Sun.COM 	    &ifinfop->if_targinfo6));
6340Sstevel@tonic-gate }
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate /*
637*8485SPeter.Memishian@Sun.COM  * Create a group; arguments match ipmp_groupinfo_t fields.  Returns a pointer
6380Sstevel@tonic-gate  * to the new group on success, or NULL on failure.
6390Sstevel@tonic-gate  */
6400Sstevel@tonic-gate ipmp_groupinfo_t *
ipmp_groupinfo_create(const char * name,uint64_t sig,uint_t fdt,ipmp_group_state_t state,uint_t nif,char (* ifs)[LIFNAMSIZ],const char * grifname,const char * m4ifname,const char * m6ifname,const char * bcifname,uint_t naddr,struct sockaddr_storage * addrs)641*8485SPeter.Memishian@Sun.COM ipmp_groupinfo_create(const char *name, uint64_t sig, uint_t fdt,
642*8485SPeter.Memishian@Sun.COM     ipmp_group_state_t state, uint_t nif, char (*ifs)[LIFNAMSIZ],
643*8485SPeter.Memishian@Sun.COM     const char *grifname, const char *m4ifname, const char *m6ifname,
644*8485SPeter.Memishian@Sun.COM     const char *bcifname, uint_t naddr, struct sockaddr_storage *addrs)
6450Sstevel@tonic-gate {
6460Sstevel@tonic-gate 	ipmp_groupinfo_t *grinfop;
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	grinfop = malloc(sizeof (ipmp_groupinfo_t));
6490Sstevel@tonic-gate 	if (grinfop == NULL)
6500Sstevel@tonic-gate 		return (NULL);
6510Sstevel@tonic-gate 
652*8485SPeter.Memishian@Sun.COM 	grinfop->gr_sig	= sig;
653*8485SPeter.Memishian@Sun.COM 	grinfop->gr_fdt = fdt;
654*8485SPeter.Memishian@Sun.COM 	grinfop->gr_state = state;
655*8485SPeter.Memishian@Sun.COM 	grinfop->gr_iflistp = ipmp_iflist_create(nif, ifs);
656*8485SPeter.Memishian@Sun.COM 	grinfop->gr_adlistp = ipmp_addrlist_create(naddr, addrs);
657*8485SPeter.Memishian@Sun.COM 	if (grinfop->gr_iflistp == NULL || grinfop->gr_adlistp == NULL) {
658*8485SPeter.Memishian@Sun.COM 		ipmp_freegroupinfo(grinfop);
6590Sstevel@tonic-gate 		return (NULL);
6600Sstevel@tonic-gate 	}
6610Sstevel@tonic-gate 	(void) strlcpy(grinfop->gr_name, name, LIFGRNAMSIZ);
662*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(grinfop->gr_ifname, grifname, LIFNAMSIZ);
663*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(grinfop->gr_m4ifname, m4ifname, LIFNAMSIZ);
664*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(grinfop->gr_m6ifname, m6ifname, LIFNAMSIZ);
665*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(grinfop->gr_bcifname, bcifname, LIFNAMSIZ);
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	return (grinfop);
6680Sstevel@tonic-gate }
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate /*
6710Sstevel@tonic-gate  * Clone the group information named by `grinfop'.  Returns a pointer to
6720Sstevel@tonic-gate  * the clone on success, or NULL on failure.
6730Sstevel@tonic-gate  */
6740Sstevel@tonic-gate ipmp_groupinfo_t *
ipmp_groupinfo_clone(ipmp_groupinfo_t * grinfop)6750Sstevel@tonic-gate ipmp_groupinfo_clone(ipmp_groupinfo_t *grinfop)
6760Sstevel@tonic-gate {
677*8485SPeter.Memishian@Sun.COM 	ipmp_addrlist_t *adlistp = grinfop->gr_adlistp;
678*8485SPeter.Memishian@Sun.COM 
6790Sstevel@tonic-gate 	return (ipmp_groupinfo_create(grinfop->gr_name, grinfop->gr_sig,
680*8485SPeter.Memishian@Sun.COM 	    grinfop->gr_fdt, grinfop->gr_state, grinfop->gr_iflistp->il_nif,
681*8485SPeter.Memishian@Sun.COM 	    grinfop->gr_iflistp->il_ifs, grinfop->gr_ifname,
682*8485SPeter.Memishian@Sun.COM 	    grinfop->gr_m4ifname, grinfop->gr_m6ifname, grinfop->gr_bcifname,
683*8485SPeter.Memishian@Sun.COM 	    adlistp->al_naddr, adlistp->al_addrs));
684*8485SPeter.Memishian@Sun.COM }
685*8485SPeter.Memishian@Sun.COM 
686*8485SPeter.Memishian@Sun.COM /*
687*8485SPeter.Memishian@Sun.COM  * Create an address list; arguments match ipmp_addrlist_t fields.  Returns
688*8485SPeter.Memishian@Sun.COM  * a pointer to the new address list on success, or NULL on failure.
689*8485SPeter.Memishian@Sun.COM  */
690*8485SPeter.Memishian@Sun.COM static ipmp_addrlist_t *
ipmp_addrlist_create(uint_t naddr,struct sockaddr_storage * addrs)691*8485SPeter.Memishian@Sun.COM ipmp_addrlist_create(uint_t naddr, struct sockaddr_storage *addrs)
692*8485SPeter.Memishian@Sun.COM {
693*8485SPeter.Memishian@Sun.COM 	unsigned int i;
694*8485SPeter.Memishian@Sun.COM 	ipmp_addrlist_t *adlistp;
695*8485SPeter.Memishian@Sun.COM 
696*8485SPeter.Memishian@Sun.COM 	adlistp = malloc(IPMP_ADDRLIST_SIZE(naddr));
697*8485SPeter.Memishian@Sun.COM 	if (adlistp == NULL)
698*8485SPeter.Memishian@Sun.COM 		return (NULL);
699*8485SPeter.Memishian@Sun.COM 
700*8485SPeter.Memishian@Sun.COM 	adlistp->al_naddr = naddr;
701*8485SPeter.Memishian@Sun.COM 	for (i = 0; i < naddr; i++)
702*8485SPeter.Memishian@Sun.COM 		adlistp->al_addrs[i] = addrs[i];
703*8485SPeter.Memishian@Sun.COM 
704*8485SPeter.Memishian@Sun.COM 	return (adlistp);
705*8485SPeter.Memishian@Sun.COM }
706*8485SPeter.Memishian@Sun.COM 
707*8485SPeter.Memishian@Sun.COM /*
708*8485SPeter.Memishian@Sun.COM  * Clone the address list named by `adlistp'.  Returns a pointer to the clone
709*8485SPeter.Memishian@Sun.COM  * on success, or NULL on failure.
710*8485SPeter.Memishian@Sun.COM  */
711*8485SPeter.Memishian@Sun.COM static ipmp_addrlist_t *
ipmp_addrlist_clone(ipmp_addrlist_t * adlistp)712*8485SPeter.Memishian@Sun.COM ipmp_addrlist_clone(ipmp_addrlist_t *adlistp)
713*8485SPeter.Memishian@Sun.COM {
714*8485SPeter.Memishian@Sun.COM 	return (ipmp_addrlist_create(adlistp->al_naddr, adlistp->al_addrs));
715*8485SPeter.Memishian@Sun.COM }
716*8485SPeter.Memishian@Sun.COM 
717*8485SPeter.Memishian@Sun.COM /*
718*8485SPeter.Memishian@Sun.COM  * Free the address list pointed to by `adlistp'.
719*8485SPeter.Memishian@Sun.COM  */
720*8485SPeter.Memishian@Sun.COM static void
ipmp_freeaddrlist(ipmp_addrlist_t * adlistp)721*8485SPeter.Memishian@Sun.COM ipmp_freeaddrlist(ipmp_addrlist_t *adlistp)
722*8485SPeter.Memishian@Sun.COM {
723*8485SPeter.Memishian@Sun.COM 	free(adlistp);
724*8485SPeter.Memishian@Sun.COM }
725*8485SPeter.Memishian@Sun.COM 
726*8485SPeter.Memishian@Sun.COM /*
727*8485SPeter.Memishian@Sun.COM  * Create an address; arguments match ipmp_addrinfo_t fields.  Returns a
728*8485SPeter.Memishian@Sun.COM  * pointer to the new address on success, or NULL on failure.
729*8485SPeter.Memishian@Sun.COM  */
730*8485SPeter.Memishian@Sun.COM ipmp_addrinfo_t *
ipmp_addrinfo_create(struct sockaddr_storage * addrp,ipmp_addr_state_t state,const char * group,const char * binding)731*8485SPeter.Memishian@Sun.COM ipmp_addrinfo_create(struct sockaddr_storage *addrp, ipmp_addr_state_t state,
732*8485SPeter.Memishian@Sun.COM     const char *group, const char *binding)
733*8485SPeter.Memishian@Sun.COM {
734*8485SPeter.Memishian@Sun.COM 	ipmp_addrinfo_t *adinfop;
735*8485SPeter.Memishian@Sun.COM 
736*8485SPeter.Memishian@Sun.COM 	adinfop = malloc(sizeof (ipmp_addrinfo_t));
737*8485SPeter.Memishian@Sun.COM 	if (adinfop == NULL)
738*8485SPeter.Memishian@Sun.COM 		return (NULL);
739*8485SPeter.Memishian@Sun.COM 
740*8485SPeter.Memishian@Sun.COM 	adinfop->ad_addr = *addrp;
741*8485SPeter.Memishian@Sun.COM 	adinfop->ad_state = state;
742*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(adinfop->ad_group, group, LIFGRNAMSIZ);
743*8485SPeter.Memishian@Sun.COM 	(void) strlcpy(adinfop->ad_binding, binding, LIFNAMSIZ);
744*8485SPeter.Memishian@Sun.COM 
745*8485SPeter.Memishian@Sun.COM 	return (adinfop);
746*8485SPeter.Memishian@Sun.COM }
747*8485SPeter.Memishian@Sun.COM 
748*8485SPeter.Memishian@Sun.COM /*
749*8485SPeter.Memishian@Sun.COM  * Clone the address information named by `adinfop'.  Returns a pointer to
750*8485SPeter.Memishian@Sun.COM  * the clone on success, or NULL on failure.
751*8485SPeter.Memishian@Sun.COM  */
752*8485SPeter.Memishian@Sun.COM ipmp_addrinfo_t *
ipmp_addrinfo_clone(ipmp_addrinfo_t * adinfop)753*8485SPeter.Memishian@Sun.COM ipmp_addrinfo_clone(ipmp_addrinfo_t *adinfop)
754*8485SPeter.Memishian@Sun.COM {
755*8485SPeter.Memishian@Sun.COM 	return (ipmp_addrinfo_create(&adinfop->ad_addr, adinfop->ad_state,
756*8485SPeter.Memishian@Sun.COM 	    adinfop->ad_group, adinfop->ad_binding));
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * Set the query context associated with `handle' to `qcontext', which must be
7610Sstevel@tonic-gate  * either IPMP_QCONTEXT_LIVE or IPMP_QCONTEXT_SNAP.  Upon success, any
7620Sstevel@tonic-gate  * previous snapshot associated with `handle' is discarded.  Returns an IPMP
7630Sstevel@tonic-gate  * error code.
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate int
ipmp_setqcontext(ipmp_handle_t handle,ipmp_qcontext_t qcontext)7660Sstevel@tonic-gate ipmp_setqcontext(ipmp_handle_t handle, ipmp_qcontext_t qcontext)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	ipmp_state_t	*statep = handle;
7690Sstevel@tonic-gate 	ipmp_snap_t	*snap;
7700Sstevel@tonic-gate 	int		retval;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	switch (qcontext) {
7730Sstevel@tonic-gate 	case IPMP_QCONTEXT_LIVE:
7740Sstevel@tonic-gate 		snap = NULL;
7750Sstevel@tonic-gate 		break;
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	case IPMP_QCONTEXT_SNAP:
7780Sstevel@tonic-gate 		retval = ipmp_snap_take(statep, &snap);
7790Sstevel@tonic-gate 		if (retval != IPMP_SUCCESS)
7800Sstevel@tonic-gate 			return (retval);
7810Sstevel@tonic-gate 		break;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	default:
7840Sstevel@tonic-gate 		return (IPMP_EINVAL);
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	if (statep->st_snap != NULL)
7880Sstevel@tonic-gate 		ipmp_snap_free(statep->st_snap);
7890Sstevel@tonic-gate 	statep->st_snap = snap;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	return (IPMP_SUCCESS);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate /*
7950Sstevel@tonic-gate  * Create an empty snapshot.  Returns a pointer to the snapshot on success,
7960Sstevel@tonic-gate  * or NULL on failure.
7970Sstevel@tonic-gate  */
7980Sstevel@tonic-gate ipmp_snap_t *
ipmp_snap_create(void)7990Sstevel@tonic-gate ipmp_snap_create(void)
8000Sstevel@tonic-gate {
8010Sstevel@tonic-gate 	ipmp_snap_t *snap;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	snap = malloc(sizeof (ipmp_snap_t));
8040Sstevel@tonic-gate 	if (snap == NULL)
8050Sstevel@tonic-gate 		return (NULL);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	snap->sn_grlistp = NULL;
8080Sstevel@tonic-gate 	snap->sn_grinfolistp = NULL;
8090Sstevel@tonic-gate 	snap->sn_ifinfolistp = NULL;
810*8485SPeter.Memishian@Sun.COM 	snap->sn_adinfolistp = NULL;
8110Sstevel@tonic-gate 	snap->sn_ngroup = 0;
8120Sstevel@tonic-gate 	snap->sn_nif = 0;
813*8485SPeter.Memishian@Sun.COM 	snap->sn_naddr = 0;
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	return (snap);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate  * Free all of the resources associated with snapshot `snap'.
8200Sstevel@tonic-gate  */
8210Sstevel@tonic-gate void
ipmp_snap_free(ipmp_snap_t * snap)8220Sstevel@tonic-gate ipmp_snap_free(ipmp_snap_t *snap)
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate 	ipmp_ifinfolist_t	*iflp, *ifnext;
825*8485SPeter.Memishian@Sun.COM 	ipmp_addrinfolist_t	*adlp, *adnext;
8260Sstevel@tonic-gate 	ipmp_groupinfolist_t	*grlp, *grnext;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 	ipmp_freegrouplist(snap->sn_grlistp);
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	for (grlp = snap->sn_grinfolistp; grlp != NULL; grlp = grnext) {
8310Sstevel@tonic-gate 		grnext = grlp->grl_next;
8320Sstevel@tonic-gate 		ipmp_freegroupinfo(grlp->grl_grinfop);
8330Sstevel@tonic-gate 		free(grlp);
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	for (iflp = snap->sn_ifinfolistp; iflp != NULL; iflp = ifnext) {
8370Sstevel@tonic-gate 		ifnext = iflp->ifl_next;
8380Sstevel@tonic-gate 		ipmp_freeifinfo(iflp->ifl_ifinfop);
8390Sstevel@tonic-gate 		free(iflp);
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 
842*8485SPeter.Memishian@Sun.COM 	for (adlp = snap->sn_adinfolistp; adlp != NULL; adlp = adnext) {
843*8485SPeter.Memishian@Sun.COM 		adnext = adlp->adl_next;
844*8485SPeter.Memishian@Sun.COM 		ipmp_freeaddrinfo(adlp->adl_adinfop);
845*8485SPeter.Memishian@Sun.COM 		free(adlp);
846*8485SPeter.Memishian@Sun.COM 	}
847*8485SPeter.Memishian@Sun.COM 
8480Sstevel@tonic-gate 	free(snap);
8490Sstevel@tonic-gate }
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate /*
8520Sstevel@tonic-gate  * Add the group information in `grinfop' to the snapshot named by `snap'.
8530Sstevel@tonic-gate  * Returns an IPMP error code.
8540Sstevel@tonic-gate  */
8550Sstevel@tonic-gate int
ipmp_snap_addgroupinfo(ipmp_snap_t * snap,ipmp_groupinfo_t * grinfop)8560Sstevel@tonic-gate ipmp_snap_addgroupinfo(ipmp_snap_t *snap, ipmp_groupinfo_t *grinfop)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	ipmp_groupinfolist_t *grlp;
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	/*
8610Sstevel@tonic-gate 	 * If the information for this group is already in the snapshot,
8620Sstevel@tonic-gate 	 * in.mpathd is broken.
8630Sstevel@tonic-gate 	 */
8640Sstevel@tonic-gate 	if (ipmp_snap_getgroupinfo(snap, grinfop->gr_name) != NULL)
8650Sstevel@tonic-gate 		return (IPMP_EPROTO);
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	grlp = malloc(sizeof (ipmp_groupinfolist_t));
8680Sstevel@tonic-gate 	if (grlp == NULL)
8690Sstevel@tonic-gate 		return (IPMP_ENOMEM);
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	grlp->grl_grinfop = grinfop;
8720Sstevel@tonic-gate 	grlp->grl_next = snap->sn_grinfolistp;
8730Sstevel@tonic-gate 	snap->sn_grinfolistp = grlp;
8740Sstevel@tonic-gate 	snap->sn_ngroup++;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 	return (IPMP_SUCCESS);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate /*
8800Sstevel@tonic-gate  * Add the interface information in `ifinfop' to the snapshot named by `snap'.
8810Sstevel@tonic-gate  * Returns an IPMP error code.
8820Sstevel@tonic-gate  */
8830Sstevel@tonic-gate int
ipmp_snap_addifinfo(ipmp_snap_t * snap,ipmp_ifinfo_t * ifinfop)8840Sstevel@tonic-gate ipmp_snap_addifinfo(ipmp_snap_t *snap, ipmp_ifinfo_t *ifinfop)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	ipmp_ifinfolist_t *iflp;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	/*
8890Sstevel@tonic-gate 	 * If the information for this interface is already in the snapshot,
8900Sstevel@tonic-gate 	 * in.mpathd is broken.
8910Sstevel@tonic-gate 	 */
8920Sstevel@tonic-gate 	if (ipmp_snap_getifinfo(snap, ifinfop->if_name) != NULL)
8930Sstevel@tonic-gate 		return (IPMP_EPROTO);
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	iflp = malloc(sizeof (ipmp_ifinfolist_t));
8960Sstevel@tonic-gate 	if (iflp == NULL)
8970Sstevel@tonic-gate 		return (IPMP_ENOMEM);
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	iflp->ifl_ifinfop = ifinfop;
9000Sstevel@tonic-gate 	iflp->ifl_next = snap->sn_ifinfolistp;
9010Sstevel@tonic-gate 	snap->sn_ifinfolistp = iflp;
9020Sstevel@tonic-gate 	snap->sn_nif++;
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	return (IPMP_SUCCESS);
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
908*8485SPeter.Memishian@Sun.COM  * Add the address information in `adinfop' to the snapshot named by `snap'.
909*8485SPeter.Memishian@Sun.COM  * Returns an IPMP error code.
910*8485SPeter.Memishian@Sun.COM  */
911*8485SPeter.Memishian@Sun.COM int
ipmp_snap_addaddrinfo(ipmp_snap_t * snap,ipmp_addrinfo_t * adinfop)912*8485SPeter.Memishian@Sun.COM ipmp_snap_addaddrinfo(ipmp_snap_t *snap, ipmp_addrinfo_t *adinfop)
913*8485SPeter.Memishian@Sun.COM {
914*8485SPeter.Memishian@Sun.COM 	ipmp_addrinfolist_t *adlp;
915*8485SPeter.Memishian@Sun.COM 
916*8485SPeter.Memishian@Sun.COM 	/*
917*8485SPeter.Memishian@Sun.COM 	 * Any duplicate addresses should've already been weeded by in.mpathd.
918*8485SPeter.Memishian@Sun.COM 	 */
919*8485SPeter.Memishian@Sun.COM 	if (ipmp_snap_getaddrinfo(snap, adinfop->ad_group,
920*8485SPeter.Memishian@Sun.COM 	    &adinfop->ad_addr) != NULL)
921*8485SPeter.Memishian@Sun.COM 		return (IPMP_EPROTO);
922*8485SPeter.Memishian@Sun.COM 
923*8485SPeter.Memishian@Sun.COM 	adlp = malloc(sizeof (ipmp_addrinfolist_t));
924*8485SPeter.Memishian@Sun.COM 	if (adlp == NULL)
925*8485SPeter.Memishian@Sun.COM 		return (IPMP_ENOMEM);
926*8485SPeter.Memishian@Sun.COM 
927*8485SPeter.Memishian@Sun.COM 	adlp->adl_adinfop = adinfop;
928*8485SPeter.Memishian@Sun.COM 	adlp->adl_next = snap->sn_adinfolistp;
929*8485SPeter.Memishian@Sun.COM 	snap->sn_adinfolistp = adlp;
930*8485SPeter.Memishian@Sun.COM 	snap->sn_naddr++;
931*8485SPeter.Memishian@Sun.COM 
932*8485SPeter.Memishian@Sun.COM 	return (IPMP_SUCCESS);
933*8485SPeter.Memishian@Sun.COM }
934*8485SPeter.Memishian@Sun.COM 
935*8485SPeter.Memishian@Sun.COM /*
9360Sstevel@tonic-gate  * Retrieve the information for the group `name' in snapshot `snap'.
9370Sstevel@tonic-gate  * Returns a pointer to the group information on success, or NULL on failure.
9380Sstevel@tonic-gate  */
9390Sstevel@tonic-gate static ipmp_groupinfo_t *
ipmp_snap_getgroupinfo(ipmp_snap_t * snap,const char * name)9400Sstevel@tonic-gate ipmp_snap_getgroupinfo(ipmp_snap_t *snap, const char *name)
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate 	ipmp_groupinfolist_t *grlp;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	for (grlp = snap->sn_grinfolistp; grlp != NULL; grlp = grlp->grl_next) {
9450Sstevel@tonic-gate 		if (strcmp(grlp->grl_grinfop->gr_name, name) == 0)
9460Sstevel@tonic-gate 			break;
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	return (grlp != NULL ? grlp->grl_grinfop : NULL);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate  * Retrieve the information for the interface `name' in snapshot `snap'.
9540Sstevel@tonic-gate  * Returns a pointer to the interface information on success, or NULL on
9550Sstevel@tonic-gate  * failure.
9560Sstevel@tonic-gate  */
9570Sstevel@tonic-gate static ipmp_ifinfo_t *
ipmp_snap_getifinfo(ipmp_snap_t * snap,const char * name)9580Sstevel@tonic-gate ipmp_snap_getifinfo(ipmp_snap_t *snap, const char *name)
9590Sstevel@tonic-gate {
9600Sstevel@tonic-gate 	ipmp_ifinfolist_t *iflp;
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	for (iflp = snap->sn_ifinfolistp; iflp != NULL; iflp = iflp->ifl_next) {
9630Sstevel@tonic-gate 		if (strcmp(iflp->ifl_ifinfop->if_name, name) == 0)
9640Sstevel@tonic-gate 			break;
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	return (iflp != NULL ? iflp->ifl_ifinfop : NULL);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
971*8485SPeter.Memishian@Sun.COM  * Retrieve the information for the address `addrp' on group `grname' in
972*8485SPeter.Memishian@Sun.COM  * snapshot `snap'.  Returns a pointer to the address information on success,
973*8485SPeter.Memishian@Sun.COM  * or NULL on failure.
974*8485SPeter.Memishian@Sun.COM  */
975*8485SPeter.Memishian@Sun.COM static ipmp_addrinfo_t *
ipmp_snap_getaddrinfo(ipmp_snap_t * snap,const char * grname,struct sockaddr_storage * addrp)976*8485SPeter.Memishian@Sun.COM ipmp_snap_getaddrinfo(ipmp_snap_t *snap, const char *grname,
977*8485SPeter.Memishian@Sun.COM     struct sockaddr_storage *addrp)
978*8485SPeter.Memishian@Sun.COM {
979*8485SPeter.Memishian@Sun.COM 	ipmp_addrinfolist_t *adlp;
980*8485SPeter.Memishian@Sun.COM 
981*8485SPeter.Memishian@Sun.COM 	for (adlp = snap->sn_adinfolistp; adlp != NULL; adlp = adlp->adl_next) {
982*8485SPeter.Memishian@Sun.COM 		if (strcmp(grname, adlp->adl_adinfop->ad_group) == 0 &&
983*8485SPeter.Memishian@Sun.COM 		    sockaddrcmp(addrp, &adlp->adl_adinfop->ad_addr))
984*8485SPeter.Memishian@Sun.COM 			break;
985*8485SPeter.Memishian@Sun.COM 	}
986*8485SPeter.Memishian@Sun.COM 
987*8485SPeter.Memishian@Sun.COM 	return (adlp != NULL ? adlp->adl_adinfop : NULL);
988*8485SPeter.Memishian@Sun.COM }
989*8485SPeter.Memishian@Sun.COM 
990*8485SPeter.Memishian@Sun.COM /*
9910Sstevel@tonic-gate  * Using `statep', take a snapshot of the IPMP subsystem and if successful
9920Sstevel@tonic-gate  * return it in a dynamically allocated snapshot pointed to by `*snapp'.
9930Sstevel@tonic-gate  * Returns an IPMP error code.
9940Sstevel@tonic-gate  */
9950Sstevel@tonic-gate static int
ipmp_snap_take(ipmp_state_t * statep,ipmp_snap_t ** snapp)9960Sstevel@tonic-gate ipmp_snap_take(ipmp_state_t *statep, ipmp_snap_t **snapp)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	ipmp_snap_t	*snap, *osnap;
9990Sstevel@tonic-gate 	ipmp_infotype_t	type;
10000Sstevel@tonic-gate 	int		retval;
10010Sstevel@tonic-gate 	size_t		len;
10020Sstevel@tonic-gate 	void		*infop;
10030Sstevel@tonic-gate 	struct timeval	end;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	snap = ipmp_snap_create();
10060Sstevel@tonic-gate 	if (snap == NULL)
10070Sstevel@tonic-gate 		return (IPMP_ENOMEM);
10080Sstevel@tonic-gate 
1009*8485SPeter.Memishian@Sun.COM 	retval = ipmp_sendquery(statep, IPMP_SNAP, NULL, NULL, &end);
10100Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS) {
10110Sstevel@tonic-gate 		ipmp_snap_free(snap);
10120Sstevel@tonic-gate 		return (retval);
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 
10150Sstevel@tonic-gate 	retval = ipmp_readinfo(statep, IPMP_SNAP, (void **)&osnap, &end);
10160Sstevel@tonic-gate 	if (retval != IPMP_SUCCESS) {
10170Sstevel@tonic-gate 		ipmp_snap_free(snap);
10180Sstevel@tonic-gate 		return (ipmp_querydone(statep, retval));
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	/*
1022*8485SPeter.Memishian@Sun.COM 	 * Using the information in the `osnap' snapshot, build up our own
1023*8485SPeter.Memishian@Sun.COM 	 * snapshot.  We know there will always be at least one TLV (for
1024*8485SPeter.Memishian@Sun.COM 	 * IPMP_GROUPLIST).  If we receive anything illogical (e.g., more than
1025*8485SPeter.Memishian@Sun.COM 	 * the expected number of interfaces), then bail out.  However, to a
1026*8485SPeter.Memishian@Sun.COM 	 * large extent we have to trust the information sent by in.mpathd.
10270Sstevel@tonic-gate 	 */
10280Sstevel@tonic-gate 	do {
10290Sstevel@tonic-gate 		infop = NULL;
10300Sstevel@tonic-gate 		retval = ipmp_readtlv(statep->st_fd, &type, &len, &infop, &end);
10310Sstevel@tonic-gate 		if (retval != IPMP_SUCCESS)
10320Sstevel@tonic-gate 			goto fail;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 		if (!ipmp_checktlv(type, len, infop)) {
10350Sstevel@tonic-gate 			retval = IPMP_EPROTO;
10360Sstevel@tonic-gate 			goto fail;
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 		switch (type) {
10400Sstevel@tonic-gate 		case IPMP_GROUPLIST:
10410Sstevel@tonic-gate 			if (snap->sn_grlistp != NULL) {
10420Sstevel@tonic-gate 				retval = IPMP_EPROTO;
10430Sstevel@tonic-gate 				break;
10440Sstevel@tonic-gate 			}
10450Sstevel@tonic-gate 			snap->sn_grlistp = infop;
10460Sstevel@tonic-gate 			break;
10470Sstevel@tonic-gate 
10480Sstevel@tonic-gate 		case IPMP_IFINFO:
10490Sstevel@tonic-gate 			if (snap->sn_nif == osnap->sn_nif) {
10500Sstevel@tonic-gate 				retval = IPMP_EPROTO;
10510Sstevel@tonic-gate 				break;
10520Sstevel@tonic-gate 			}
1053*8485SPeter.Memishian@Sun.COM 
1054*8485SPeter.Memishian@Sun.COM 			/*
1055*8485SPeter.Memishian@Sun.COM 			 * Read in V4 and V6 targlist TLVs that follow.
1056*8485SPeter.Memishian@Sun.COM 			 */
1057*8485SPeter.Memishian@Sun.COM 			retval = ipmp_readifinfo_lists(statep, infop, &end);
1058*8485SPeter.Memishian@Sun.COM 			if (retval != IPMP_SUCCESS)
1059*8485SPeter.Memishian@Sun.COM 				break;
1060*8485SPeter.Memishian@Sun.COM 
10610Sstevel@tonic-gate 			retval = ipmp_snap_addifinfo(snap, infop);
1062*8485SPeter.Memishian@Sun.COM 			if (retval != IPMP_SUCCESS) {
1063*8485SPeter.Memishian@Sun.COM 				ipmp_freeifinfo(infop);
1064*8485SPeter.Memishian@Sun.COM 				infop = NULL;
1065*8485SPeter.Memishian@Sun.COM 			}
1066*8485SPeter.Memishian@Sun.COM 			break;
1067*8485SPeter.Memishian@Sun.COM 
1068*8485SPeter.Memishian@Sun.COM 		case IPMP_ADDRINFO:
1069*8485SPeter.Memishian@Sun.COM 			if (snap->sn_naddr == osnap->sn_naddr) {
1070*8485SPeter.Memishian@Sun.COM 				retval = IPMP_EPROTO;
1071*8485SPeter.Memishian@Sun.COM 				break;
1072*8485SPeter.Memishian@Sun.COM 			}
1073*8485SPeter.Memishian@Sun.COM 
1074*8485SPeter.Memishian@Sun.COM 			retval = ipmp_snap_addaddrinfo(snap, infop);
1075*8485SPeter.Memishian@Sun.COM 			/*
1076*8485SPeter.Memishian@Sun.COM 			 * NOTE: since we didn't call ipmp_read*info_lists(),
1077*8485SPeter.Memishian@Sun.COM 			 * no need to use ipmp_freeaddrinfo() on failure.
1078*8485SPeter.Memishian@Sun.COM 			 */
10790Sstevel@tonic-gate 			break;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		case IPMP_GROUPINFO:
10820Sstevel@tonic-gate 			if (snap->sn_ngroup == osnap->sn_ngroup) {
10830Sstevel@tonic-gate 				retval = IPMP_EPROTO;
10840Sstevel@tonic-gate 				break;
10850Sstevel@tonic-gate 			}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 			/*
1088*8485SPeter.Memishian@Sun.COM 			 * Read in IPMP groupinfo list TLVs that follow.
10890Sstevel@tonic-gate 			 */
1090*8485SPeter.Memishian@Sun.COM 			retval = ipmp_readgroupinfo_lists(statep, infop, &end);
10910Sstevel@tonic-gate 			if (retval != IPMP_SUCCESS)
10920Sstevel@tonic-gate 				break;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 			retval = ipmp_snap_addgroupinfo(snap, infop);
1095*8485SPeter.Memishian@Sun.COM 			if (retval != IPMP_SUCCESS) {
1096*8485SPeter.Memishian@Sun.COM 				ipmp_freegroupinfo(infop);
1097*8485SPeter.Memishian@Sun.COM 				infop = NULL;
1098*8485SPeter.Memishian@Sun.COM 			}
10990Sstevel@tonic-gate 			break;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 		default:
11020Sstevel@tonic-gate 			retval = IPMP_EPROTO;
11030Sstevel@tonic-gate 			break;
11040Sstevel@tonic-gate 		}
11050Sstevel@tonic-gate fail:
11060Sstevel@tonic-gate 		if (retval != IPMP_SUCCESS) {
11070Sstevel@tonic-gate 			free(infop);
11080Sstevel@tonic-gate 			free(osnap);
11090Sstevel@tonic-gate 			ipmp_snap_free(snap);
11100Sstevel@tonic-gate 			return (ipmp_querydone(statep, retval));
11110Sstevel@tonic-gate 		}
11120Sstevel@tonic-gate 	} while (snap->sn_grlistp == NULL || snap->sn_nif < osnap->sn_nif ||
1113*8485SPeter.Memishian@Sun.COM 	    snap->sn_ngroup < osnap->sn_ngroup ||
1114*8485SPeter.Memishian@Sun.COM 	    snap->sn_naddr < osnap->sn_naddr);
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	free(osnap);
11170Sstevel@tonic-gate 	*snapp = snap;
11180Sstevel@tonic-gate 	return (ipmp_querydone(statep, IPMP_SUCCESS));
11190Sstevel@tonic-gate }
1120