1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/sockio.h>
30 #include <sys/sysevent/vrrp.h>
31 #include <sys/sysevent/eventdefs.h>
32 #include <sys/varargs.h>
33 #include <auth_attr.h>
34 #include <ctype.h>
35 #include <fcntl.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 #include <errno.h>
39 #include <unistd.h>
40 #include <zone.h>
41 #include <libsysevent.h>
42 #include <limits.h>
43 #include <locale.h>
44 #include <inetcfg.h>
45 #include <arpa/inet.h>
46 #include <signal.h>
47 #include <assert.h>
48 #include <ucred.h>
49 #include <bsm/adt.h>
50 #include <bsm/adt_event.h>
51 #include <priv_utils.h>
52 #include <libdllink.h>
53 #include <libdlvnic.h>
54 #include <pwd.h>
55 #include <libvrrpadm.h>
56 #include <net/route.h>
57 #include "vrrpd_impl.h"
58 
59 /*
60  * A VRRP router can be only start participating the VRRP protocol of a virtual
61  * router when all the following conditions are met:
62  *
63  * - The VRRP router is enabled (vr->vvr_conf.vvc_enabled is _B_TRUE)
64  * - The RX socket is successfully created over the physical interface to
65  *   receive the VRRP multicast advertisement. Note that one RX socket can
66  *   be shared by several VRRP routers configured over the same physical
67  *   interface. (See vrrpd_init_rxsock())
68  * - The TX socket is successfully created over the VNIC interface to send
69  *   the VRRP advertisment. (See vrrpd_init_txsock())
70  * - The primary IP address has been successfully selected over the physical
71  *   interface. (See vrrpd_select_primary())
72  *
73  * If a VRRP router is enabled but the other conditions haven't be satisfied,
74  * the router will be stay at the VRRP_STATE_INIT state. If all the above
75  * conditions are met, the VRRP router will be transit to either
76  * the VRRP_STATE_MASTER or the VRRP_STATE_BACKUP state, depends on the VRRP
77  * protocol.
78  */
79 
80 #define	skip_whitespace(p)	while (isspace(*(p))) ++(p)
81 
82 #define	BUFFSIZE	65536
83 
84 #define	VRRPCONF	"/etc/inet/vrrp.conf"
85 
86 typedef struct vrrpd_rtsock_s {
87 	int		vrt_af;		/* address family */
88 	int		vrt_fd;		/* socket for the PF_ROUTE msg */
89 	iu_event_id_t	vrt_eid;	/* event ID */
90 } vrrpd_rtsock_t;
91 
92 static int		vrrp_logflag = 0;
93 boolean_t		vrrp_debug_level = 0;
94 iu_eh_t			*vrrpd_eh = NULL;
95 iu_tq_t			*vrrpd_timerq = NULL;
96 static vrrp_handle_t	vrrpd_vh = NULL;
97 static int		vrrpd_cmdsock_fd = -1;	/* socket to communicate */
98 						/* between vrrpd/libvrrpadm */
99 static iu_event_id_t	vrrpd_cmdsock_eid = -1;
100 static int		vrrpd_ctlsock_fd = -1;	/* socket to bring up/down */
101 						/* the virtual IP addresses */
102 static int		vrrpd_ctlsock6_fd = -1;
103 static vrrpd_rtsock_t	vrrpd_rtsocks[2] = {
104 	{AF_INET, -1, -1},
105 	{AF_INET6, -1, -1}
106 };
107 static iu_timer_id_t	vrrp_scan_timer_id = -1;
108 
109 TAILQ_HEAD(vrrp_vr_list_s, vrrp_vr_s);
110 TAILQ_HEAD(vrrp_intf_list_s, vrrp_intf_s);
111 static struct vrrp_vr_list_s	vrrp_vr_list;
112 static struct vrrp_intf_list_s	vrrp_intf_list;
113 static char		vrrpd_conffile[MAXPATHLEN];
114 
115 /*
116  * Multicast address of VRRP advertisement in network byte order
117  */
118 static vrrp_addr_t	vrrp_muladdr4;
119 static vrrp_addr_t	vrrp_muladdr6;
120 
121 static int		vrrpd_scan_interval = 20000;	/* ms */
122 static int		pfds[2];
123 
124 /*
125  * macros to calculate skew_time and master_down_timer
126  *
127  * Note that the input is in centisecs and output are in msecs
128  */
129 #define	SKEW_TIME(pri, intv)	((intv) * (256 - (pri)) / 256)
130 #define	MASTER_DOWN_INTERVAL(pri, intv)	(3 * (intv) + SKEW_TIME((pri), (intv)))
131 
132 #define	SKEW_TIME_VR(vr)	\
133 	SKEW_TIME((vr)->vvr_conf.vvc_pri, (vr)->vvr_master_adver_int)
134 #define	MASTER_DOWN_INTERVAL_VR(vr)	\
135 	MASTER_DOWN_INTERVAL((vr)->vvr_conf.vvc_pri, (vr)->vvr_master_adver_int)
136 
137 #define	VRRP_CONF_UPDATE	0x01
138 #define	VRRP_CONF_DELETE	0x02
139 
140 static char *af_str(int);
141 
142 static iu_tq_callback_t vrrp_adv_timeout;
143 static iu_tq_callback_t vrrp_b2m_timeout;
144 static iu_eh_callback_t vrrpd_sock_handler;
145 static iu_eh_callback_t vrrpd_rtsock_handler;
146 static iu_eh_callback_t vrrpd_cmdsock_handler;
147 
148 static int daemon_init();
149 
150 static vrrp_err_t vrrpd_init();
151 static void vrrpd_fini();
152 static vrrp_err_t vrrpd_cmdsock_create();
153 static void vrrpd_cmdsock_destroy();
154 static vrrp_err_t vrrpd_rtsock_create();
155 static void vrrpd_rtsock_destroy();
156 static vrrp_err_t vrrpd_ctlsock_create();
157 static void vrrpd_ctlsock_destroy();
158 
159 static void vrrpd_scan_timer(iu_tq_t *, void *);
160 static void vrrpd_scan(int);
161 static vrrp_err_t vrrpd_init_rxsock(vrrp_vr_t *);
162 static void vrrpd_fini_rxsock(vrrp_vr_t *);
163 static vrrp_err_t vrrpd_init_txsock(vrrp_vr_t *);
164 static vrrp_err_t vrrpd_init_txsock_v4(vrrp_vr_t *);
165 static vrrp_err_t vrrpd_init_txsock_v6(vrrp_vr_t *);
166 static void vrrpd_fini_txsock(vrrp_vr_t *);
167 
168 static vrrp_err_t vrrpd_create_vr(vrrp_vr_conf_t *);
169 static vrrp_err_t vrrpd_enable_vr(vrrp_vr_t *);
170 static void vrrpd_disable_vr(vrrp_vr_t *, vrrp_intf_t *, boolean_t);
171 static void vrrpd_delete_vr(vrrp_vr_t *);
172 
173 static vrrp_err_t vrrpd_create(vrrp_vr_conf_t *, boolean_t);
174 static vrrp_err_t vrrpd_delete(const char *);
175 static vrrp_err_t vrrpd_enable(const char *, boolean_t);
176 static vrrp_err_t vrrpd_disable(const char *);
177 static vrrp_err_t vrrpd_modify(vrrp_vr_conf_t *, uint32_t);
178 static void vrrpd_list(vrid_t, char *, int, vrrp_ret_list_t *, size_t *);
179 static void vrrpd_query(const char *, vrrp_ret_query_t *, size_t *);
180 
181 static boolean_t vrrp_rd_prop_name(vrrp_vr_conf_t *, const char *);
182 static boolean_t vrrp_rd_prop_vrid(vrrp_vr_conf_t *, const char *);
183 static boolean_t vrrp_rd_prop_af(vrrp_vr_conf_t *, const char *);
184 static boolean_t vrrp_rd_prop_pri(vrrp_vr_conf_t *, const char *);
185 static boolean_t vrrp_rd_prop_adver_int(vrrp_vr_conf_t *, const char *);
186 static boolean_t vrrp_rd_prop_preempt(vrrp_vr_conf_t *, const char *);
187 static boolean_t vrrp_rd_prop_accept(vrrp_vr_conf_t *, const char *);
188 static boolean_t vrrp_rd_prop_ifname(vrrp_vr_conf_t *, const char *);
189 static boolean_t vrrp_rd_prop_enabled(vrrp_vr_conf_t *, const char *);
190 static int vrrp_wt_prop_name(vrrp_vr_conf_t *, char *, size_t);
191 static int vrrp_wt_prop_vrid(vrrp_vr_conf_t *, char *, size_t);
192 static int vrrp_wt_prop_af(vrrp_vr_conf_t *, char *, size_t);
193 static int vrrp_wt_prop_pri(vrrp_vr_conf_t *, char *, size_t);
194 static int vrrp_wt_prop_adver_int(vrrp_vr_conf_t *, char *, size_t);
195 static int vrrp_wt_prop_preempt(vrrp_vr_conf_t *, char *, size_t);
196 static int vrrp_wt_prop_accept(vrrp_vr_conf_t *, char *, size_t);
197 static int vrrp_wt_prop_ifname(vrrp_vr_conf_t *, char *, size_t);
198 static int vrrp_wt_prop_enabled(vrrp_vr_conf_t *, char *, size_t);
199 
200 static void vrrpd_cmd_create(void *, void *, size_t *);
201 static void vrrpd_cmd_delete(void *, void *, size_t *);
202 static void vrrpd_cmd_enable(void *, void *, size_t *);
203 static void vrrpd_cmd_disable(void *, void *, size_t *);
204 static void vrrpd_cmd_modify(void *, void *, size_t *);
205 static void vrrpd_cmd_list(void *, void *, size_t *);
206 static void vrrpd_cmd_query(void *, void *, size_t *);
207 
208 static vrrp_vr_t *vrrpd_lookup_vr_by_vrid(char *, vrid_t vrid_t, int);
209 static vrrp_vr_t *vrrpd_lookup_vr_by_name(const char *);
210 static vrrp_intf_t *vrrpd_lookup_if(const char *, int);
211 static vrrp_err_t vrrpd_create_if(const char *, int, uint32_t, vrrp_intf_t **);
212 static void vrrpd_delete_if(vrrp_intf_t *, boolean_t);
213 static vrrp_err_t vrrpd_create_ip(vrrp_intf_t *, const char *, vrrp_addr_t *,
214     uint64_t flags);
215 static void vrrpd_delete_ip(vrrp_intf_t *, vrrp_ip_t *);
216 
217 static void vrrpd_init_ipcache(int);
218 static void vrrpd_update_ipcache(int);
219 static int vrrpd_walk_ipaddr(icfg_if_t *, void *);
220 static vrrp_err_t vrrpd_add_ipaddr(char *, int, vrrp_addr_t *,
221     int, uint64_t);
222 static vrrp_ip_t *vrrpd_select_primary(vrrp_intf_t *);
223 static void vrrpd_reselect_primary(vrrp_intf_t *);
224 static void vrrpd_reenable_all_vr();
225 static void vrrpd_remove_if(vrrp_intf_t *, boolean_t);
226 
227 static uint16_t in_cksum(int, uint16_t, void *);
228 static uint16_t vrrp_cksum4(struct in_addr *, struct in_addr *,
229     uint16_t, vrrp_pkt_t *);
230 static uint16_t vrrp_cksum6(struct in6_addr *, struct in6_addr *,
231     uint16_t, vrrp_pkt_t *);
232 static size_t vrrpd_build_vrrp(vrrp_vr_t *, uchar_t *, int, boolean_t);
233 
234 static void vrrpd_process_adv(vrrp_vr_t *, vrrp_addr_t *, vrrp_pkt_t *);
235 static vrrp_err_t vrrpd_send_adv(vrrp_vr_t *, boolean_t);
236 
237 /* state transition functions */
238 static vrrp_err_t vrrpd_state_i2m(vrrp_vr_t *);
239 static vrrp_err_t vrrpd_state_i2b(vrrp_vr_t *);
240 static void vrrpd_state_m2i(vrrp_vr_t *);
241 static void vrrpd_state_b2i(vrrp_vr_t *);
242 static vrrp_err_t vrrpd_state_b2m(vrrp_vr_t *);
243 static vrrp_err_t vrrpd_state_m2b(vrrp_vr_t *);
244 static void vrrpd_state_trans(vrrp_state_t, vrrp_state_t, vrrp_vr_t *);
245 
246 static vrrp_err_t vrrpd_set_noaccept(vrrp_vr_t *, boolean_t);
247 static vrrp_err_t vrrpd_virtualip_update(vrrp_vr_t *, boolean_t);
248 static vrrp_err_t vrrpd_virtualip_updateone(vrrp_intf_t *, vrrp_ip_t *,
249     boolean_t);
250 static int vrrpd_post_event(const char *, vrrp_state_t, vrrp_state_t);
251 
252 static void vrrpd_initconf();
253 static vrrp_err_t vrrpd_updateconf(vrrp_vr_conf_t *, uint_t);
254 static vrrp_err_t vrrpd_write_vrconf(char *, size_t, vrrp_vr_conf_t *);
255 static vrrp_err_t vrrpd_read_vrconf(char *, vrrp_vr_conf_t *);
256 static vrrp_err_t vrrpd_readprop(const char *, vrrp_vr_conf_t *);
257 static void vrrpd_cleanup();
258 
259 static void vrrp_log(int, char *, ...);
260 static int timeval_to_milli(struct timeval);
261 static struct timeval timeval_delta(struct timeval, struct timeval);
262 
263 typedef struct vrrpd_prop_s {
264 	char		*vs_propname;
265 	boolean_t	(*vs_propread)(vrrp_vr_conf_t *, const char *);
266 	int		(*vs_propwrite)(vrrp_vr_conf_t *, char *, size_t);
267 } vrrp_prop_t;
268 
269 /*
270  * persistent VRRP properties array
271  */
272 static vrrp_prop_t vrrp_prop_info_tbl[] = {
273 	{"name", vrrp_rd_prop_name, vrrp_wt_prop_name},
274 	{"vrid", vrrp_rd_prop_vrid, vrrp_wt_prop_vrid},
275 	{"priority", vrrp_rd_prop_pri, vrrp_wt_prop_pri},
276 	{"adv_intval", vrrp_rd_prop_adver_int, vrrp_wt_prop_adver_int},
277 	{"preempt_mode", vrrp_rd_prop_preempt, vrrp_wt_prop_preempt},
278 	{"accept_mode", vrrp_rd_prop_accept, vrrp_wt_prop_accept},
279 	{"interface", vrrp_rd_prop_ifname, vrrp_wt_prop_ifname},
280 	{"af", vrrp_rd_prop_af, vrrp_wt_prop_af},
281 	{"enabled", vrrp_rd_prop_enabled, vrrp_wt_prop_enabled}
282 };
283 
284 #define	VRRP_PROP_INFO_TABSIZE	\
285 	(sizeof (vrrp_prop_info_tbl) / sizeof (vrrp_prop_t))
286 
287 typedef void vrrp_cmd_func_t(void *, void *, size_t *);
288 
289 typedef struct vrrp_cmd_info_s {
290 	vrrp_cmd_type_t	vi_cmd;
291 	size_t		vi_reqsize;
292 	size_t		vi_acksize;	/* 0 if the size is variable */
293 	boolean_t	vi_setop;	/* Set operation? Check credentials */
294 	vrrp_cmd_func_t	*vi_cmdfunc;
295 } vrrp_cmd_info_t;
296 
297 static vrrp_cmd_info_t vrrp_cmd_info_tbl[] = {
298 	{VRRP_CMD_CREATE, sizeof (vrrp_cmd_create_t),
299 	    sizeof (vrrp_ret_create_t), _B_TRUE, vrrpd_cmd_create},
300 	{VRRP_CMD_DELETE, sizeof (vrrp_cmd_delete_t),
301 	    sizeof (vrrp_ret_delete_t), _B_TRUE, vrrpd_cmd_delete},
302 	{VRRP_CMD_ENABLE, sizeof (vrrp_cmd_enable_t),
303 	    sizeof (vrrp_ret_enable_t), _B_TRUE, vrrpd_cmd_enable},
304 	{VRRP_CMD_DISABLE, sizeof (vrrp_cmd_disable_t),
305 	    sizeof (vrrp_ret_disable_t), _B_TRUE, vrrpd_cmd_disable},
306 	{VRRP_CMD_MODIFY, sizeof (vrrp_cmd_modify_t),
307 	    sizeof (vrrp_ret_modify_t), _B_TRUE, vrrpd_cmd_modify},
308 	{VRRP_CMD_QUERY, sizeof (vrrp_cmd_query_t), 0,
309 	    _B_FALSE, vrrpd_cmd_query},
310 	{VRRP_CMD_LIST, sizeof (vrrp_cmd_list_t), 0,
311 	    _B_FALSE, vrrpd_cmd_list}
312 };
313 
314 #define	VRRP_DOOR_INFO_TABLE_SIZE	\
315 	(sizeof (vrrp_cmd_info_tbl) / sizeof (vrrp_cmd_info_t))
316 
317 static int
318 ipaddr_cmp(int af, vrrp_addr_t *addr1, vrrp_addr_t *addr2)
319 {
320 	if (af == AF_INET) {
321 		return (memcmp(&addr1->in4.sin_addr,
322 		    &addr2->in4.sin_addr, sizeof (struct in_addr)));
323 	} else {
324 		return (memcmp(&addr1->in6.sin6_addr,
325 		    &addr2->in6.sin6_addr, sizeof (struct in6_addr)));
326 	}
327 }
328 
329 static vrrp_vr_t *
330 vrrpd_lookup_vr_by_vrid(char *ifname, vrid_t vrid, int af)
331 {
332 	vrrp_vr_t *vr;
333 
334 	TAILQ_FOREACH(vr, &vrrp_vr_list, vvr_next) {
335 		if (strcmp(vr->vvr_conf.vvc_link, ifname) == 0 &&
336 		    vr->vvr_conf.vvc_vrid == vrid &&
337 		    vr->vvr_conf.vvc_af == af) {
338 			break;
339 		}
340 	}
341 	return (vr);
342 }
343 
344 static vrrp_vr_t *
345 vrrpd_lookup_vr_by_name(const char *name)
346 {
347 	vrrp_vr_t *vr;
348 
349 	TAILQ_FOREACH(vr, &vrrp_vr_list, vvr_next) {
350 		if (strcmp(vr->vvr_conf.vvc_name, name) == 0)
351 			break;
352 	}
353 	return (vr);
354 }
355 
356 static vrrp_intf_t *
357 vrrpd_lookup_if(const char *ifname, int af)
358 {
359 	vrrp_intf_t	*intf;
360 
361 	TAILQ_FOREACH(intf, &vrrp_intf_list, vvi_next) {
362 		if (strcmp(ifname, intf->vvi_ifname) == 0 &&
363 		    af == intf->vvi_af) {
364 			break;
365 		}
366 	}
367 	return (intf);
368 }
369 
370 static vrrp_err_t
371 vrrpd_create_if(const char *ifname, int af, uint32_t ifindex,
372     vrrp_intf_t **intfp)
373 {
374 	vrrp_intf_t	*intf;
375 
376 	vrrp_log(VRRP_DBG0, "vrrpd_create_if(%s, %s, %d)",
377 	    ifname, af_str(af), ifindex);
378 
379 	if (((*intfp) = malloc(sizeof (vrrp_intf_t))) == NULL) {
380 		vrrp_log(VRRP_ERR, "vrrpd_create_if(): failed to "
381 		    "allocate %s/%s interface", ifname, af_str(af));
382 		return (VRRP_ENOMEM);
383 	}
384 
385 	intf = *intfp;
386 	TAILQ_INIT(&intf->vvi_iplist);
387 	(void) strlcpy(intf->vvi_ifname, ifname, sizeof (intf->vvi_ifname));
388 	intf->vvi_af = af;
389 	intf->vvi_sockfd = -1;
390 	intf->vvi_nvr = 0;
391 	intf->vvi_eid = -1;
392 	intf->vvi_pip = NULL;
393 	intf->vvi_ifindex = ifindex;
394 	intf->vvi_state = NODE_STATE_NEW;
395 	intf->vvi_vr_state = VRRP_STATE_INIT;
396 	TAILQ_INSERT_TAIL(&vrrp_intf_list, intf, vvi_next);
397 	return (VRRP_SUCCESS);
398 }
399 
400 /*
401  * An interface is deleted. If update_vr is true, the deletion of the interface
402  * may cause the state transition of assoicated VRRP router (if this interface
403  * is either the primary or the VNIC interface of the VRRP router); otherwise,
404  * simply delete the interface without updating the VRRP router.
405  */
406 static void
407 vrrpd_delete_if(vrrp_intf_t *intf, boolean_t update_vr)
408 {
409 	vrrp_ip_t	*ip;
410 
411 	vrrp_log(VRRP_DBG0, "vrrpd_delete_if(%s, %s, %supdate_vr)",
412 	    intf->vvi_ifname, af_str(intf->vvi_af), update_vr ? "" : "no_");
413 
414 	if (update_vr) {
415 		/*
416 		 * If a this interface is the physical interface or the VNIC
417 		 * of a VRRP router, the deletion of the interface (no IP
418 		 * address exists on this interface) may cause the state
419 		 * transition of the VRRP router. call vrrpd_remove_if()
420 		 * to find all corresponding VRRP router and update their
421 		 * states.
422 		 */
423 		vrrpd_remove_if(intf, _B_FALSE);
424 	}
425 
426 	/*
427 	 * First remove and delete all the IP addresses on the interface
428 	 */
429 	while (!TAILQ_EMPTY(&intf->vvi_iplist)) {
430 		ip = TAILQ_FIRST(&intf->vvi_iplist);
431 		vrrpd_delete_ip(intf, ip);
432 	}
433 
434 	/*
435 	 * Then remove and delete the interface
436 	 */
437 	TAILQ_REMOVE(&vrrp_intf_list, intf, vvi_next);
438 	(void) free(intf);
439 }
440 
441 static vrrp_err_t
442 vrrpd_create_ip(vrrp_intf_t *intf, const char *lifname, vrrp_addr_t *addr,
443     uint64_t flags)
444 {
445 	vrrp_ip_t	*ip;
446 	char		abuf[INET6_ADDRSTRLEN];
447 
448 	/* LINTED E_CONSTANT_CONDITION */
449 	VRRPADDR2STR(intf->vvi_af, addr, abuf, INET6_ADDRSTRLEN, _B_FALSE);
450 	vrrp_log(VRRP_DBG0, "vrrpd_create_ip(%s, %s, %s, 0x%x)",
451 	    intf->vvi_ifname, lifname, abuf, flags);
452 
453 	if ((ip = malloc(sizeof (vrrp_ip_t))) == NULL) {
454 		vrrp_log(VRRP_ERR, "vrrpd_create_ip(%s, %s):"
455 		    "failed to allocate IP", lifname, abuf);
456 		return (VRRP_ENOMEM);
457 	}
458 
459 	(void) strncpy(ip->vip_lifname, lifname, sizeof (ip->vip_lifname));
460 	ip->vip_state = NODE_STATE_NEW;
461 	ip->vip_flags = flags;
462 	(void) memcpy(&ip->vip_addr, addr, sizeof (ip->vip_addr));
463 
464 	/*
465 	 * Make sure link-local IPv6 IP addresses are at the head of the list
466 	 */
467 	if (intf->vvi_af == AF_INET6 &&
468 	    IN6_IS_ADDR_LINKLOCAL(&addr->in6.sin6_addr)) {
469 		TAILQ_INSERT_HEAD(&intf->vvi_iplist, ip, vip_next);
470 	} else {
471 		TAILQ_INSERT_TAIL(&intf->vvi_iplist, ip, vip_next);
472 	}
473 	return (VRRP_SUCCESS);
474 }
475 
476 static void
477 vrrpd_delete_ip(vrrp_intf_t *intf, vrrp_ip_t *ip)
478 {
479 	char	abuf[INET6_ADDRSTRLEN];
480 	int	af = intf->vvi_af;
481 
482 	/* LINTED E_CONSTANT_CONDITION */
483 	VRRPADDR2STR(af, &ip->vip_addr, abuf, sizeof (abuf), _B_FALSE);
484 	vrrp_log(VRRP_DBG0, "vrrpd_delete_ip(%s, %s, %s) is %sprimary",
485 	    intf->vvi_ifname, ip->vip_lifname, abuf,
486 	    intf->vvi_pip == ip ? "" : "not ");
487 
488 	if (intf->vvi_pip == ip)
489 		intf->vvi_pip = NULL;
490 
491 	TAILQ_REMOVE(&intf->vvi_iplist, ip, vip_next);
492 	(void) free(ip);
493 }
494 
495 static char *
496 rtm_event2str(uchar_t event)
497 {
498 	switch (event) {
499 	case RTM_NEWADDR:
500 		return ("RTM_NEWADDR");
501 	case RTM_DELADDR:
502 		return ("RTM_DELADDR");
503 	case RTM_IFINFO:
504 		return ("RTM_IFINFO");
505 	case RTM_ADD:
506 		return ("RTM_ADD");
507 	case RTM_DELETE:
508 		return ("RTM_DELETE");
509 	case RTM_CHANGE:
510 		return ("RTM_CHANGE");
511 	case RTM_OLDADD:
512 		return ("RTM_OLDADD");
513 	case RTM_OLDDEL:
514 		return ("RTM_OLDDEL");
515 	case RTM_CHGADDR:
516 		return ("RTM_CHGADDR");
517 	case RTM_FREEADDR:
518 		return ("RTM_FREEADDR");
519 	default:
520 		return ("RTM_OTHER");
521 	}
522 }
523 
524 /*
525  * This is called by the child process to inform the parent process to
526  * exit with the given return value. Note that the child process
527  * (the daemon process) informs the parent process to exit when anything
528  * goes wrong or when all the intialization is done.
529  */
530 static int
531 vrrpd_inform_parent_exit(int rv)
532 {
533 	int err = 0;
534 
535 	/*
536 	 * If vrrp_debug_level is none-zero, vrrpd is not running as
537 	 * a daemon. Return directly.
538 	 */
539 	if (vrrp_debug_level != 0)
540 		return (0);
541 
542 	if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
543 		err = errno;
544 		(void) close(pfds[1]);
545 		return (err);
546 	}
547 	(void) close(pfds[1]);
548 	return (0);
549 }
550 
551 int
552 main(int argc, char *argv[])
553 {
554 	int c, err;
555 	struct sigaction sa;
556 	sigset_t mask;
557 	struct rlimit rl;
558 
559 	(void) setlocale(LC_ALL, "");
560 	(void) textdomain(TEXT_DOMAIN);
561 
562 	/*
563 	 * We need PRIV_SYS_CONFIG to post VRRP sysevent, PRIV_NET_RAWACESS
564 	 * and PRIV_NET_ICMPACCESS to open  the raw socket, PRIV_SYS_IP_CONFIG
565 	 * to bring up/down the virtual IP addresses, and PRIV_SYS_RESOURCE to
566 	 * setrlimit().
567 	 *
568 	 * Note that sysevent is not supported in non-global zones.
569 	 */
570 	if (getzoneid() == GLOBAL_ZONEID) {
571 		err = __init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, 0, 0,
572 		    PRIV_SYS_CONFIG, PRIV_NET_RAWACCESS, PRIV_NET_ICMPACCESS,
573 		    PRIV_SYS_IP_CONFIG, PRIV_SYS_RESOURCE, NULL);
574 	} else {
575 		err = __init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, 0, 0,
576 		    PRIV_NET_RAWACCESS, PRIV_NET_ICMPACCESS,
577 		    PRIV_SYS_IP_CONFIG, PRIV_SYS_RESOURCE, NULL);
578 	}
579 
580 	if (err == -1) {
581 		vrrp_log(VRRP_ERR, "main(): init_daemon_priv() failed");
582 		return (EXIT_FAILURE);
583 	}
584 
585 	/*
586 	 * If vrrpd is started by other process, it will inherit the
587 	 * signal block mask. We unblock all signals to make sure the
588 	 * signal handling will work normally.
589 	 */
590 	(void) sigfillset(&mask);
591 	(void) thr_sigsetmask(SIG_UNBLOCK, &mask, NULL);
592 	sa.sa_handler = vrrpd_cleanup;
593 	sa.sa_flags = 0;
594 	(void) sigemptyset(&sa.sa_mask);
595 	(void) sigaction(SIGINT, &sa, NULL);
596 	(void) sigaction(SIGQUIT, &sa, NULL);
597 	(void) sigaction(SIGTERM, &sa, NULL);
598 
599 	vrrp_debug_level = 0;
600 	(void) strlcpy(vrrpd_conffile, VRRPCONF, sizeof (vrrpd_conffile));
601 	while ((c = getopt(argc, argv, "d:f:")) != EOF) {
602 		switch (c) {
603 		case 'd':
604 			vrrp_debug_level = atoi(optarg);
605 			break;
606 		case 'f':
607 			(void) strlcpy(vrrpd_conffile, optarg,
608 			    sizeof (vrrpd_conffile));
609 			break;
610 		default:
611 			break;
612 		}
613 	}
614 
615 	closefrom(3);
616 	if (vrrp_debug_level == 0 && (daemon_init() != 0)) {
617 		vrrp_log(VRRP_ERR, "main(): daemon_init() failed");
618 		return (EXIT_FAILURE);
619 	}
620 
621 	rl.rlim_cur = RLIM_INFINITY;
622 	rl.rlim_max = RLIM_INFINITY;
623 	if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
624 		vrrp_log(VRRP_ERR, "main(): setrlimit() failed");
625 		goto child_out;
626 	}
627 
628 	if (vrrpd_init() != VRRP_SUCCESS) {
629 		vrrp_log(VRRP_ERR, "main(): vrrpd_init() failed");
630 		goto child_out;
631 	}
632 
633 	/*
634 	 * Get rid of unneeded privileges.
635 	 */
636 	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
637 	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, PRIV_SYS_RESOURCE, NULL);
638 
639 	/*
640 	 * Read the configuration and initialize the existing VRRP
641 	 * configuration
642 	 */
643 	vrrpd_initconf();
644 
645 	/*
646 	 * Inform the parent process that it can successfully exit.
647 	 */
648 	if ((err = vrrpd_inform_parent_exit(EXIT_SUCCESS)) != 0) {
649 		vrrpd_cleanup();
650 		vrrp_log(VRRP_WARNING, "vrrpd_inform_parent_exit() failed: %s",
651 		    strerror(err));
652 		return (EXIT_FAILURE);
653 	}
654 
655 	/*
656 	 * Start the loop to handle the timer and the IO events.
657 	 */
658 	switch (iu_handle_events(vrrpd_eh, vrrpd_timerq)) {
659 	case -1:
660 		vrrp_log(VRRP_ERR, "main(): iu_handle_events() failed "
661 		    "abnormally");
662 		break;
663 	default:
664 		break;
665 	}
666 
667 	vrrpd_cleanup();
668 	return (EXIT_SUCCESS);
669 
670 child_out:
671 	(void) vrrpd_inform_parent_exit(EXIT_FAILURE);
672 	return (EXIT_FAILURE);
673 }
674 
675 static int
676 daemon_init()
677 {
678 	pid_t	pid;
679 	int	rv;
680 
681 	vrrp_log(VRRP_DBG0, "daemon_init()");
682 
683 	if (getenv("SMF_FMRI") == NULL) {
684 		vrrp_log(VRRP_ERR, "daemon_init(): vrrpd is an smf(5) managed "
685 		    "service and should not be run from the command line.");
686 		return (-1);
687 	}
688 
689 	/*
690 	 * Create the pipe used for the child process to inform the parent
691 	 * process to exit after all initialization is done.
692 	 */
693 	if (pipe(pfds) < 0) {
694 		vrrp_log(VRRP_ERR, "daemon_init(): pipe() failed: %s",
695 		    strerror(errno));
696 		return (-1);
697 	}
698 
699 	if ((pid = fork()) < 0) {
700 		vrrp_log(VRRP_ERR, "daemon_init(): fork() failed: %s",
701 		    strerror(errno));
702 		(void) close(pfds[0]);
703 		(void) close(pfds[1]);
704 		return (-1);
705 	}
706 
707 	if (pid != 0) { /* Parent */
708 		(void) close(pfds[1]);
709 
710 		/*
711 		 * Read the child process's return value from the pfds.
712 		 * If the child process exits unexpectedly, read() returns -1.
713 		 */
714 		if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
715 			vrrp_log(VRRP_ERR, "daemon_init(): child process "
716 			    "exited unexpectedly %s", strerror(errno));
717 			(void) kill(pid, SIGTERM);
718 			rv = EXIT_FAILURE;
719 		}
720 		(void) close(pfds[0]);
721 		exit(rv);
722 	}
723 
724 	/*
725 	 * in child process, became a daemon, and return to main() to continue.
726 	 */
727 	(void) close(pfds[0]);
728 	(void) chdir("/");
729 	(void) setsid();
730 	(void) close(0);
731 	(void) close(1);
732 	(void) close(2);
733 	(void) open("/dev/null", O_RDWR, 0);
734 	(void) dup2(0, 1);
735 	(void) dup2(0, 2);
736 	openlog("vrrpd", LOG_PID, LOG_DAEMON);
737 	vrrp_logflag = 1;
738 	return (0);
739 }
740 
741 static vrrp_err_t
742 vrrpd_init()
743 {
744 	vrrp_err_t	err = VRRP_ESYS;
745 
746 	vrrp_log(VRRP_DBG0, "vrrpd_init()");
747 
748 	TAILQ_INIT(&vrrp_vr_list);
749 	TAILQ_INIT(&vrrp_intf_list);
750 
751 	if (vrrp_open(&vrrpd_vh) != VRRP_SUCCESS) {
752 		vrrp_log(VRRP_ERR, "vrrpd_init(): vrrp_open() failed");
753 		goto fail;
754 	}
755 
756 	if ((vrrpd_timerq = iu_tq_create()) == NULL) {
757 		vrrp_log(VRRP_ERR, "vrrpd_init(): iu_tq_create() failed");
758 		goto fail;
759 	}
760 
761 	if ((vrrpd_eh = iu_eh_create()) == NULL) {
762 		vrrp_log(VRRP_ERR, "vrrpd_init(): iu_eh_create() failed");
763 		goto fail;
764 	}
765 
766 	/*
767 	 * Create the AF_UNIX socket used to communicate with libvrrpadm.
768 	 *
769 	 * This socket is used to receive the administrative requests and
770 	 * send back the results.
771 	 */
772 	if (vrrpd_cmdsock_create() != VRRP_SUCCESS) {
773 		vrrp_log(VRRP_ERR, "vrrpd_init(): vrrpd_cmdsock_create() "
774 		    "failed");
775 		goto fail;
776 	}
777 
778 	/*
779 	 * Create the VRRP control socket used to bring up/down the virtual
780 	 * IP addresses. It is also used to set the IFF_NOACCEPT flag of
781 	 * the virtual IP addresses.
782 	 */
783 	if (vrrpd_ctlsock_create() != VRRP_SUCCESS) {
784 		vrrp_log(VRRP_ERR, "vrrpd_init(): vrrpd_ctlsock_create() "
785 		    "failed");
786 		goto fail;
787 	}
788 
789 	/*
790 	 * Create the PF_ROUTER socket used to listen to the routing socket
791 	 * messages and build the interface/IP address list.
792 	 */
793 	if (vrrpd_rtsock_create() != VRRP_SUCCESS) {
794 		vrrp_log(VRRP_ERR, "vrrpd_init(): vrrpd_rtsock_create() "
795 		    "failed");
796 		goto fail;
797 	}
798 
799 	/*
800 	 * Build the list of interfaces and IP addresses. Also, start the time
801 	 * to scan the interfaces/IP addresses periodically.
802 	 */
803 	vrrpd_scan(AF_INET);
804 	vrrpd_scan(AF_INET6);
805 	if ((vrrp_scan_timer_id = iu_schedule_timer_ms(vrrpd_timerq,
806 	    vrrpd_scan_interval, vrrpd_scan_timer, NULL)) == -1) {
807 		vrrp_log(VRRP_ERR, "vrrpd_init(): start scan_timer failed");
808 		goto fail;
809 	}
810 
811 	/*
812 	 * Initialize the VRRP multicast address.
813 	 */
814 	bzero(&vrrp_muladdr4, sizeof (vrrp_addr_t));
815 	vrrp_muladdr4.in4.sin_family = AF_INET;
816 	(void) inet_pton(AF_INET, "224.0.0.18", &vrrp_muladdr4.in4.sin_addr);
817 
818 	bzero(&vrrp_muladdr6, sizeof (vrrp_addr_t));
819 	vrrp_muladdr6.in6.sin6_family = AF_INET6;
820 	(void) inet_pton(AF_INET6, "ff02::12", &vrrp_muladdr6.in6.sin6_addr);
821 
822 	return (VRRP_SUCCESS);
823 
824 fail:
825 	vrrpd_fini();
826 	return (err);
827 }
828 
829 static void
830 vrrpd_fini()
831 {
832 	vrrp_log(VRRP_DBG0, "vrrpd_fini()");
833 
834 	(void) iu_cancel_timer(vrrpd_timerq, vrrp_scan_timer_id, NULL);
835 	vrrp_scan_timer_id = -1;
836 
837 	vrrpd_rtsock_destroy();
838 	vrrpd_ctlsock_destroy();
839 	vrrpd_cmdsock_destroy();
840 
841 	if (vrrpd_eh != NULL) {
842 		iu_eh_destroy(vrrpd_eh);
843 		vrrpd_eh = NULL;
844 	}
845 
846 	if (vrrpd_timerq != NULL) {
847 		iu_tq_destroy(vrrpd_timerq);
848 		vrrpd_timerq = NULL;
849 	}
850 
851 	vrrp_close(vrrpd_vh);
852 	vrrpd_vh = NULL;
853 	assert(TAILQ_EMPTY(&vrrp_vr_list));
854 	assert(TAILQ_EMPTY(&vrrp_intf_list));
855 }
856 
857 static void
858 vrrpd_cleanup(void)
859 {
860 	vrrp_vr_t	*vr;
861 	vrrp_intf_t	*intf;
862 
863 	vrrp_log(VRRP_DBG0, "vrrpd_cleanup()");
864 
865 	while (!TAILQ_EMPTY(&vrrp_vr_list)) {
866 		vr = TAILQ_FIRST(&vrrp_vr_list);
867 		vrrpd_delete_vr(vr);
868 	}
869 
870 	while (!TAILQ_EMPTY(&vrrp_intf_list)) {
871 		intf = TAILQ_FIRST(&vrrp_intf_list);
872 		vrrpd_delete_if(intf, _B_FALSE);
873 	}
874 
875 	vrrpd_fini();
876 	closelog();
877 	exit(1);
878 }
879 
880 /*
881  * Read the configuration file and initialize all the existing VRRP routers.
882  */
883 static void
884 vrrpd_initconf()
885 {
886 	FILE *fp;
887 	char line[LINE_MAX];
888 	int linenum = 0;
889 	vrrp_vr_conf_t conf;
890 	vrrp_err_t err;
891 
892 	vrrp_log(VRRP_DBG0, "vrrpd_initconf()");
893 
894 	if ((fp = fopen(vrrpd_conffile, "rF")) == NULL) {
895 		vrrp_log(VRRP_ERR, "failed to open the configuration file %s",
896 		    vrrpd_conffile);
897 		return;
898 	}
899 
900 	while (fgets(line, sizeof (line), fp) != NULL) {
901 		linenum++;
902 		conf.vvc_vrid = VRRP_VRID_NONE;
903 		if ((err = vrrpd_read_vrconf(line, &conf)) != VRRP_SUCCESS) {
904 			vrrp_log(VRRP_ERR, "failed to parse %d line %s",
905 			    linenum, line);
906 			continue;
907 		}
908 
909 		/*
910 		 * Blank or comment line
911 		 */
912 		if (conf.vvc_vrid == VRRP_VRID_NONE)
913 			continue;
914 
915 		/*
916 		 * No need to update the configuration since the VRRP router
917 		 * created/enabled based on the existing configuration.
918 		 */
919 		if ((err = vrrpd_create(&conf, _B_FALSE)) != VRRP_SUCCESS) {
920 			vrrp_log(VRRP_ERR, "VRRP router %s creation failed: "
921 			    "%s", conf.vvc_name, vrrp_err2str(err));
922 			continue;
923 		}
924 
925 		if (conf.vvc_enabled &&
926 		    ((err = vrrpd_enable(conf.vvc_name, _B_FALSE)) !=
927 		    VRRP_SUCCESS)) {
928 			vrrp_log(VRRP_ERR, "VRRP router %s enable failed: %s",
929 			    conf.vvc_name, vrrp_err2str(err));
930 		}
931 	}
932 
933 	(void) fclose(fp);
934 }
935 
936 /*
937  * Create the AF_UNIX socket used to communicate with libvrrpadm.
938  *
939  * This socket is used to receive the administrative request and
940  * send back the results.
941  */
942 static vrrp_err_t
943 vrrpd_cmdsock_create()
944 {
945 	iu_event_id_t		eid;
946 	struct sockaddr_un	laddr;
947 	int			sock, flags;
948 
949 	vrrp_log(VRRP_DBG0, "vrrpd_cmdsock_create()");
950 
951 	if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
952 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_create(): socket(AF_UNIX) "
953 		    "failed: %s", strerror(errno));
954 		return (VRRP_ESYS);
955 	}
956 
957 	/*
958 	 * Set it to be non-blocking.
959 	 */
960 	flags = fcntl(sock, F_GETFL, 0);
961 	(void) fcntl(sock, F_SETFL, (flags | O_NONBLOCK));
962 
963 	/*
964 	 * Unlink first in case a previous daemon instance exited ungracefully.
965 	 */
966 	(void) unlink(VRRPD_SOCKET);
967 
968 	bzero(&laddr, sizeof (laddr));
969 	laddr.sun_family = AF_UNIX;
970 	(void) strlcpy(laddr.sun_path, VRRPD_SOCKET, sizeof (laddr.sun_path));
971 	if (bind(sock, (struct sockaddr *)&laddr, sizeof (laddr)) < 0) {
972 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_create(): bind() failed: %s",
973 		    strerror(errno));
974 		(void) close(sock);
975 		return (VRRP_ESYS);
976 	}
977 
978 	if (listen(sock, 30) < 0) {
979 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_create(): listen() "
980 		    "failed: %s", strerror(errno));
981 		(void) close(sock);
982 		return (VRRP_ESYS);
983 	}
984 
985 	if ((eid = iu_register_event(vrrpd_eh, sock, POLLIN,
986 	    vrrpd_cmdsock_handler, NULL)) == -1) {
987 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_create(): iu_register_event()"
988 		    " failed");
989 		(void) close(sock);
990 		return (VRRP_ESYS);
991 	}
992 
993 	vrrpd_cmdsock_fd = sock;
994 	vrrpd_cmdsock_eid = eid;
995 	return (VRRP_SUCCESS);
996 }
997 
998 static void
999 vrrpd_cmdsock_destroy()
1000 {
1001 	vrrp_log(VRRP_DBG0, "vrrpd_cmdsock_destroy()");
1002 
1003 	(void) iu_unregister_event(vrrpd_eh, vrrpd_cmdsock_eid, NULL);
1004 	(void) close(vrrpd_cmdsock_fd);
1005 	vrrpd_cmdsock_fd = -1;
1006 	vrrpd_cmdsock_eid = -1;
1007 }
1008 
1009 /*
1010  * Create the PF_ROUTER sockets used to listen to the routing socket
1011  * messages and build the interface/IP address list. Create one for
1012  * each address family (IPv4 and IPv6).
1013  */
1014 static vrrp_err_t
1015 vrrpd_rtsock_create()
1016 {
1017 	int		i, flags, sock;
1018 	iu_event_id_t	eid;
1019 
1020 	vrrp_log(VRRP_DBG0, "vrrpd_rtsock_create()");
1021 
1022 	for (i = 0; i < 2; i++) {
1023 		sock = socket(PF_ROUTE, SOCK_RAW, vrrpd_rtsocks[i].vrt_af);
1024 		if (sock == -1) {
1025 			vrrp_log(VRRP_ERR, "vrrpd_rtsock_create(): socket() "
1026 			    "failed: %s", strerror(errno));
1027 			break;
1028 		}
1029 
1030 		/*
1031 		 * Set it to be non-blocking.
1032 		 */
1033 		if ((flags = fcntl(sock, F_GETFL, 0)) < 0) {
1034 			vrrp_log(VRRP_ERR, "vrrpd_rtsock_create(): "
1035 			    "fcntl(F_GETFL) failed: %s", strerror(errno));
1036 			break;
1037 		}
1038 
1039 		if ((fcntl(sock, F_SETFL, flags | O_NONBLOCK)) < 0) {
1040 			vrrp_log(VRRP_ERR, "vrrpd_rtsock_create(): "
1041 			    "fcntl(F_SETFL) failed: %s", strerror(errno));
1042 			break;
1043 		}
1044 
1045 		if ((eid = iu_register_event(vrrpd_eh, sock, POLLIN,
1046 		    vrrpd_rtsock_handler, &(vrrpd_rtsocks[i].vrt_af))) == -1) {
1047 			vrrp_log(VRRP_ERR, "vrrpd_rtsock_create(): register "
1048 			    "rtsock %d(%s) failed", sock,
1049 			    af_str(vrrpd_rtsocks[i].vrt_af));
1050 			break;
1051 		}
1052 
1053 		vrrpd_rtsocks[i].vrt_fd = sock;
1054 		vrrpd_rtsocks[i].vrt_eid = eid;
1055 	}
1056 
1057 	if (i != 2) {
1058 		(void) close(sock);
1059 		vrrpd_rtsock_destroy();
1060 		return (VRRP_ESYS);
1061 	}
1062 
1063 	return (VRRP_SUCCESS);
1064 }
1065 
1066 static void
1067 vrrpd_rtsock_destroy()
1068 {
1069 	int		i;
1070 
1071 	vrrp_log(VRRP_DBG0, "vrrpd_rtsock_destroy()");
1072 	for (i = 0; i < 2; i++) {
1073 		(void) iu_unregister_event(vrrpd_eh, vrrpd_rtsocks[i].vrt_eid,
1074 		    NULL);
1075 		(void) close(vrrpd_rtsocks[i].vrt_fd);
1076 		vrrpd_rtsocks[i].vrt_eid = -1;
1077 		vrrpd_rtsocks[i].vrt_fd = -1;
1078 	}
1079 }
1080 
1081 /*
1082  * Create the VRRP control socket used to bring up/down the virtual
1083  * IP addresses. It is also used to set the IFF_NOACCEPT flag of
1084  * the virtual IP addresses.
1085  */
1086 static vrrp_err_t
1087 vrrpd_ctlsock_create()
1088 {
1089 	int	s, s6;
1090 	int	on = _B_TRUE;
1091 
1092 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1093 		vrrp_log(VRRP_ERR, "vrrpd_ctlsock_create(): socket(INET) "
1094 		    "failed: %s", strerror(errno));
1095 		return (VRRP_ESYS);
1096 	}
1097 	if (setsockopt(s, SOL_SOCKET, SO_VRRP, &on, sizeof (on)) < 0) {
1098 		vrrp_log(VRRP_ERR, "vrrpd_ctlsock_create(): "
1099 		    "setsockopt(INET, SO_VRRP) failed: %s", strerror(errno));
1100 		(void) close(s);
1101 		return (VRRP_ESYS);
1102 	}
1103 
1104 	if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1105 		vrrp_log(VRRP_ERR, "vrrpd_ctlsock_create(): socket(INET6) "
1106 		    "failed: %s", strerror(errno));
1107 		(void) close(s);
1108 		return (VRRP_ESYS);
1109 	}
1110 	if (setsockopt(s6, SOL_SOCKET, SO_VRRP, &on, sizeof (on)) < 0) {
1111 		vrrp_log(VRRP_ERR, "vrrpd_ctlsock_create(): "
1112 		    "setsockopt(INET6, SO_VRRP) failed: %s", strerror(errno));
1113 		(void) close(s);
1114 		(void) close(s6);
1115 		return (VRRP_ESYS);
1116 	}
1117 
1118 	vrrpd_ctlsock_fd = s;
1119 	vrrpd_ctlsock6_fd = s6;
1120 	return (VRRP_SUCCESS);
1121 }
1122 
1123 static void
1124 vrrpd_ctlsock_destroy()
1125 {
1126 	(void) close(vrrpd_ctlsock_fd);
1127 	vrrpd_ctlsock_fd = -1;
1128 	(void) close(vrrpd_ctlsock6_fd);
1129 	vrrpd_ctlsock6_fd = -1;
1130 }
1131 
1132 /*ARGSUSED*/
1133 static void
1134 vrrpd_cmd_create(void *arg1, void *arg2, size_t *arg2_sz)
1135 {
1136 	vrrp_cmd_create_t	*cmd = (vrrp_cmd_create_t *)arg1;
1137 	vrrp_ret_create_t	*ret = (vrrp_ret_create_t *)arg2;
1138 	vrrp_err_t		err;
1139 
1140 	err = vrrpd_create(&cmd->vcc_conf, _B_TRUE);
1141 	if (err == VRRP_SUCCESS && cmd->vcc_conf.vvc_enabled) {
1142 		/*
1143 		 * No need to update the configuration since it is already
1144 		 * done in the above vrrpd_create() call
1145 		 */
1146 		err = vrrpd_enable(cmd->vcc_conf.vvc_name, _B_FALSE);
1147 		if (err != VRRP_SUCCESS)
1148 			(void) vrrpd_delete(cmd->vcc_conf.vvc_name);
1149 	}
1150 	ret->vrc_err = err;
1151 }
1152 
1153 /*ARGSUSED*/
1154 static void
1155 vrrpd_cmd_delete(void *arg1, void *arg2, size_t *arg2_sz)
1156 {
1157 	vrrp_cmd_delete_t	*cmd = (vrrp_cmd_delete_t *)arg1;
1158 	vrrp_ret_delete_t	*ret = (vrrp_ret_delete_t *)arg2;
1159 
1160 	ret->vrd_err = vrrpd_delete(cmd->vcd_name);
1161 }
1162 
1163 /*ARGSUSED*/
1164 static void
1165 vrrpd_cmd_enable(void *arg1, void *arg2, size_t *arg2_sz)
1166 {
1167 	vrrp_cmd_enable_t	*cmd = (vrrp_cmd_enable_t *)arg1;
1168 	vrrp_ret_enable_t	*ret = (vrrp_ret_enable_t *)arg2;
1169 
1170 	ret->vrs_err = vrrpd_enable(cmd->vcs_name, _B_TRUE);
1171 }
1172 
1173 /*ARGSUSED*/
1174 static void
1175 vrrpd_cmd_disable(void *arg1, void *arg2, size_t *arg2_sz)
1176 {
1177 	vrrp_cmd_disable_t	*cmd = (vrrp_cmd_disable_t *)arg1;
1178 	vrrp_ret_disable_t	*ret = (vrrp_ret_disable_t *)arg2;
1179 
1180 	ret->vrx_err = vrrpd_disable(cmd->vcx_name);
1181 }
1182 
1183 /*ARGSUSED*/
1184 static void
1185 vrrpd_cmd_modify(void *arg1, void *arg2, size_t *arg2_sz)
1186 {
1187 	vrrp_cmd_modify_t	*cmd = (vrrp_cmd_modify_t *)arg1;
1188 	vrrp_ret_modify_t	*ret = (vrrp_ret_modify_t *)arg2;
1189 
1190 	ret->vrm_err = vrrpd_modify(&cmd->vcm_conf, cmd->vcm_mask);
1191 }
1192 
1193 static void
1194 vrrpd_cmd_query(void *arg1, void *arg2, size_t *arg2_sz)
1195 {
1196 	vrrp_cmd_query_t	*cmd = (vrrp_cmd_query_t *)arg1;
1197 
1198 	vrrpd_query(cmd->vcq_name, arg2, arg2_sz);
1199 }
1200 
1201 static void
1202 vrrpd_cmd_list(void *arg1, void *arg2, size_t *arg2_sz)
1203 {
1204 	vrrp_cmd_list_t	*cmd = (vrrp_cmd_list_t *)arg1;
1205 
1206 	vrrpd_list(cmd->vcl_vrid, cmd->vcl_ifname, cmd->vcl_af, arg2, arg2_sz);
1207 }
1208 
1209 /*
1210  * Write-type requeset must have the solaris.network.vrrp authorization.
1211  */
1212 static boolean_t
1213 vrrp_auth_check(int connfd, vrrp_cmd_info_t *cinfo)
1214 {
1215 	ucred_t		*cred = NULL;
1216 	uid_t		uid;
1217 	struct passwd	*pw;
1218 	boolean_t	success = _B_FALSE;
1219 
1220 	vrrp_log(VRRP_DBG0, "vrrp_auth_check()");
1221 
1222 	if (!cinfo->vi_setop)
1223 		return (_B_TRUE);
1224 
1225 	/*
1226 	 * Validate the credential
1227 	 */
1228 	if (getpeerucred(connfd, &cred) == (uid_t)-1) {
1229 		vrrp_log(VRRP_ERR, "vrrp_auth_check(): getpeerucred() "
1230 		    "failed: %s", strerror(errno));
1231 		return (_B_FALSE);
1232 	}
1233 
1234 	if ((uid = ucred_getruid((const ucred_t *)cred)) == (uid_t)-1) {
1235 		vrrp_log(VRRP_ERR, "vrrp_auth_check(): ucred_getruid() "
1236 		    "failed: %s", strerror(errno));
1237 		goto done;
1238 	}
1239 
1240 	if ((pw = getpwuid(uid)) == NULL) {
1241 		vrrp_log(VRRP_ERR, "vrrp_auth_check(): getpwuid() failed");
1242 		goto done;
1243 	}
1244 
1245 	success = (chkauthattr("solaris.network.vrrp", pw->pw_name) == 1);
1246 
1247 done:
1248 	ucred_free(cred);
1249 	return (success);
1250 }
1251 
1252 /*
1253  * Process the administrative request from libvrrpadm
1254  */
1255 /* ARGSUSED */
1256 static void
1257 vrrpd_cmdsock_handler(iu_eh_t *eh, int s, short events, iu_event_id_t id,
1258     void *arg)
1259 {
1260 	vrrp_cmd_info_t		*cinfo = NULL;
1261 	vrrp_err_t		err = VRRP_SUCCESS;
1262 	uchar_t			buf[BUFFSIZE], ackbuf[BUFFSIZE];
1263 	size_t			cursize, acksize, len;
1264 	uint32_t		cmd;
1265 	int			connfd, i;
1266 	struct sockaddr_in	from;
1267 	socklen_t		fromlen;
1268 
1269 	vrrp_log(VRRP_DBG0, "vrrpd_cmdsock_handler()");
1270 
1271 	fromlen = (socklen_t)sizeof (from);
1272 	if ((connfd = accept(s, (struct sockaddr *)&from, &fromlen)) < 0) {
1273 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_handler() accept(): %s",
1274 		    strerror(errno));
1275 		return;
1276 	}
1277 
1278 	/*
1279 	 * First get the type of the request
1280 	 */
1281 	cursize = 0;
1282 	while (cursize < sizeof (uint32_t)) {
1283 		len = read(connfd, buf + cursize,
1284 		    sizeof (uint32_t) - cursize);
1285 		if (len == (size_t)-1 && (errno == EAGAIN || errno == EINTR)) {
1286 			continue;
1287 		} else if (len > 0) {
1288 			cursize += len;
1289 			continue;
1290 		}
1291 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_handler(): invalid message "
1292 		    "length");
1293 		(void) close(connfd);
1294 		return;
1295 	}
1296 
1297 	/* LINTED E_BAD_PTR_CAST_ALIGN */
1298 	cmd = ((vrrp_cmd_t *)buf)->vc_cmd;
1299 	for (i = 0; i < VRRP_DOOR_INFO_TABLE_SIZE; i++) {
1300 		if (vrrp_cmd_info_tbl[i].vi_cmd == cmd) {
1301 			cinfo = vrrp_cmd_info_tbl + i;
1302 			break;
1303 		}
1304 	}
1305 
1306 	if (cinfo == NULL) {
1307 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_handler(): invalid request "
1308 		    "type %d", cmd);
1309 		err = VRRP_EINVAL;
1310 		goto done;
1311 	}
1312 
1313 	/*
1314 	 * Get the rest of the request.
1315 	 */
1316 	assert(cursize == sizeof (uint32_t));
1317 	while (cursize < cinfo->vi_reqsize) {
1318 		len = read(connfd, buf + cursize,
1319 		    cinfo->vi_reqsize - cursize);
1320 		if (len == (size_t)-1 && (errno == EAGAIN || errno == EINTR)) {
1321 			continue;
1322 		} else if (len > 0) {
1323 			cursize += len;
1324 			continue;
1325 		}
1326 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_handler(): invalid message "
1327 		    "length");
1328 		err = VRRP_EINVAL;
1329 		goto done;
1330 	}
1331 
1332 	/*
1333 	 * Validate the authorization
1334 	 */
1335 	if (!vrrp_auth_check(connfd, cinfo)) {
1336 		vrrp_log(VRRP_ERR, "vrrpd_cmdsock_handler(): "
1337 		    "not sufficient authorization");
1338 		err = VRRP_EPERM;
1339 	}
1340 
1341 done:
1342 	/*
1343 	 * Ack the request
1344 	 */
1345 	if (err != 0) {
1346 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1347 		((vrrp_ret_t *)ackbuf)->vr_err = err;
1348 		acksize = sizeof (vrrp_ret_t);
1349 	} else {
1350 		/*
1351 		 * If the size of ack is varied, the cmdfunc callback
1352 		 * will set the right size.
1353 		 */
1354 		if ((acksize = cinfo->vi_acksize) == 0)
1355 			acksize = sizeof (ackbuf);
1356 
1357 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1358 		cinfo->vi_cmdfunc((vrrp_cmd_t *)buf, ackbuf, &acksize);
1359 	}
1360 
1361 	/*
1362 	 * Send the ack back.
1363 	 */
1364 	cursize = 0;
1365 	while (cursize < acksize) {
1366 		len = sendto(connfd, ackbuf + cursize, acksize - cursize,
1367 		    0, (struct sockaddr *)&from, fromlen);
1368 		if (len == (size_t)-1 && errno == EAGAIN) {
1369 			continue;
1370 		} else if (len > 0) {
1371 			cursize += len;
1372 			continue;
1373 		} else {
1374 			vrrp_log(VRRP_ERR, "vrrpd_cmdsock_handler() failed to "
1375 			    "ack: %s", strerror(errno));
1376 			break;
1377 		}
1378 	}
1379 
1380 	(void) shutdown(connfd, SHUT_RDWR);
1381 	(void) close(connfd);
1382 }
1383 
1384 /*
1385  * Process the routing socket messages and update the interfaces/IP addresses
1386  * list
1387  */
1388 /* ARGSUSED */
1389 static void
1390 vrrpd_rtsock_handler(iu_eh_t *eh, int s, short events,
1391     iu_event_id_t id, void *arg)
1392 {
1393 	char			buf[BUFFSIZE];
1394 	struct ifa_msghdr	*ifam;
1395 	int			nbytes;
1396 	int			af = *(int *)arg;
1397 	boolean_t		scanif = _B_FALSE;
1398 
1399 	for (;;) {
1400 		nbytes = read(s, buf, sizeof (buf));
1401 		if (nbytes <= 0) {
1402 			/* No more messages */
1403 			break;
1404 		}
1405 
1406 		/* LINTED E_BAD_PTR_CAST_ALIGN */
1407 		ifam = (struct ifa_msghdr *)buf;
1408 		if (ifam->ifam_version != RTM_VERSION) {
1409 			vrrp_log(VRRP_ERR, "vrrpd_rtsock_handler(): version %d "
1410 			    "not understood", ifam->ifam_version);
1411 			break;
1412 		}
1413 
1414 		vrrp_log(VRRP_DBG0, "vrrpd_rtsock_handler(): recv %s event",
1415 		    rtm_event2str(ifam->ifam_type));
1416 
1417 		switch (ifam->ifam_type) {
1418 		case RTM_FREEADDR:
1419 		case RTM_CHGADDR:
1420 		case RTM_NEWADDR:
1421 		case RTM_DELADDR:
1422 			/*
1423 			 * An IP address has been created/updated/deleted or
1424 			 * brought up/down, re-initilialize the interface/IP
1425 			 * address list.
1426 			 */
1427 			scanif = _B_TRUE;
1428 			break;
1429 		default:
1430 			/* Not interesting */
1431 			break;
1432 		}
1433 	}
1434 
1435 	if (scanif)
1436 		vrrpd_scan(af);
1437 }
1438 
1439 /*
1440  * Periodically scan the interface/IP addresses on the system.
1441  */
1442 /* ARGSUSED */
1443 static void
1444 vrrpd_scan_timer(iu_tq_t *tq, void *arg)
1445 {
1446 	vrrp_log(VRRP_DBG0, "vrrpd_scan_timer()");
1447 	vrrpd_scan(AF_INET);
1448 	vrrpd_scan(AF_INET6);
1449 }
1450 
1451 /*
1452  * Get the list of the interface/IP addresses of the specified address
1453  * family.
1454  */
1455 static void
1456 vrrpd_scan(int af)
1457 {
1458 	vrrp_log(VRRP_DBG0, "vrrpd_scan(%s)", af_str(af));
1459 
1460 again:
1461 	vrrpd_init_ipcache(af);
1462 
1463 	/*
1464 	 * If interface index changes, walk again.
1465 	 */
1466 	if (icfg_iterate_if(af, ICFG_PLUMBED, NULL,
1467 	    vrrpd_walk_ipaddr) != ICFG_SUCCESS)
1468 		goto again;
1469 
1470 	vrrpd_update_ipcache(af);
1471 }
1472 
1473 /*
1474  * First mark all IP addresses of the specific address family to be removed.
1475  * This flag will then be cleared when we walk up all the IP addresses.
1476  */
1477 static void
1478 vrrpd_init_ipcache(int af)
1479 {
1480 	vrrp_intf_t	*intf, *next_intf;
1481 	vrrp_ip_t	*ip, *nextip;
1482 	char		abuf[INET6_ADDRSTRLEN];
1483 
1484 	vrrp_log(VRRP_DBG0, "vrrpd_init_ipcache(%s)", af_str(af));
1485 
1486 	next_intf = TAILQ_FIRST(&vrrp_intf_list);
1487 	while ((intf = next_intf) != NULL) {
1488 		next_intf = TAILQ_NEXT(intf, vvi_next);
1489 		if (intf->vvi_af != af)
1490 			continue;
1491 
1492 		/*
1493 		 * If the interface is still marked as new, it means that this
1494 		 * vrrpd_init_ipcache() call is a result of ifindex change,
1495 		 * which causes the re-walk of all the interfaces (see
1496 		 * vrrpd_add_ipaddr()), and some interfaces are still marked
1497 		 * as new during the last walk. In this case, delete this
1498 		 * interface with the "update_vr" argument to be _B_FALSE,
1499 		 * since no VRRP router has been assoicated with this
1500 		 * interface yet (the association is done in
1501 		 * vrrpd_update_ipcache()).
1502 		 *
1503 		 * This interface will be re-added later if it still exists.
1504 		 */
1505 		if (intf->vvi_state == NODE_STATE_NEW) {
1506 			vrrp_log(VRRP_DBG0, "vrrpd_init_ipcache(): remove %s "
1507 			    "(%d), may be added later", intf->vvi_ifname,
1508 			    intf->vvi_ifindex);
1509 			vrrpd_delete_if(intf, _B_FALSE);
1510 			continue;
1511 		}
1512 
1513 		for (ip = TAILQ_FIRST(&intf->vvi_iplist); ip != NULL;
1514 		    ip = nextip) {
1515 			nextip = TAILQ_NEXT(ip, vip_next);
1516 			/* LINTED E_CONSTANT_CONDITION */
1517 			VRRPADDR2STR(af, &ip->vip_addr, abuf,
1518 			    INET6_ADDRSTRLEN, _B_FALSE);
1519 
1520 			if (ip->vip_state != NODE_STATE_NEW) {
1521 				vrrp_log(VRRP_DBG0, "vrrpd_init_ipcache(%s/%d, "
1522 				    "%s(%s/0x%x))", intf->vvi_ifname,
1523 				    intf->vvi_ifindex, ip->vip_lifname,
1524 				    abuf, ip->vip_flags);
1525 				ip->vip_state = NODE_STATE_STALE;
1526 				continue;
1527 			}
1528 
1529 			/*
1530 			 * If the IP is still marked as new, it means that
1531 			 * this vrrpd_init_ipcache() call is a result of
1532 			 * ifindex change, which causes the re-walk of all
1533 			 * the IP addresses (see vrrpd_add_ipaddr()).
1534 			 * Delete this IP.
1535 			 *
1536 			 * This IP will be readded later if it still exists.
1537 			 */
1538 			vrrp_log(VRRP_DBG0, "vrrpd_init_ipcache(): remove "
1539 			    "%s/%d , %s(%s)", intf->vvi_ifname,
1540 			    intf->vvi_ifindex, ip->vip_lifname, abuf);
1541 			vrrpd_delete_ip(intf, ip);
1542 		}
1543 	}
1544 }
1545 
1546 /*
1547  * Walk all the IP addresses on the given interface and update its
1548  * addresses list. Return ICFG_FAILURE if it is required to walk
1549  * all the interfaces again (one of the interface index changes in between).
1550  */
1551 /* ARGSUSED */
1552 static int
1553 vrrpd_walk_ipaddr(icfg_if_t *intf, void *arg)
1554 {
1555 	icfg_handle_t	ih;
1556 	int		ifindex;
1557 	vrrp_addr_t	addr;
1558 	socklen_t	addrlen = (socklen_t)sizeof (struct sockaddr_in6);
1559 	int		prefixlen;
1560 	uint64_t	flags;
1561 	int		err = ICFG_SUCCESS;
1562 
1563 	vrrp_log(VRRP_DBG0, "vrrpd_walk_ipaddr(%s, %s)", intf->if_name,
1564 	    af_str(intf->if_protocol));
1565 
1566 	if (icfg_open(&ih, intf) != ICFG_SUCCESS) {
1567 		vrrp_log(VRRP_ERR, "vrrpd_walk_ipaddr(%s, %s): icfg_open() "
1568 		    "failed: %s", intf->if_name, af_str(intf->if_protocol),
1569 		    strerror(errno));
1570 		return (err);
1571 	}
1572 
1573 	if (icfg_get_flags(ih, &flags) != ICFG_SUCCESS) {
1574 		if (errno != ENXIO && errno != ENOENT) {
1575 			vrrp_log(VRRP_ERR, "vrrpd_walk_ipaddr(%s, %s): "
1576 			    "icfg_get_flags() failed %s", intf->if_name,
1577 			    af_str(intf->if_protocol), strerror(errno));
1578 		}
1579 		goto done;
1580 	}
1581 
1582 	/*
1583 	 * skip virtual/IPMP/P2P interfaces.
1584 	 */
1585 	if ((flags & (IFF_VIRTUAL|IFF_IPMP|IFF_POINTOPOINT)) != 0) {
1586 		vrrp_log(VRRP_DBG0, "vrrpd_walk_ipaddr(%s, %s) skipped",
1587 		    intf->if_name, af_str(intf->if_protocol));
1588 		goto done;
1589 	}
1590 
1591 	if (icfg_get_index(ih, &ifindex) != ICFG_SUCCESS) {
1592 		if (errno != ENXIO && errno != ENOENT) {
1593 			vrrp_log(VRRP_ERR, "vrrpd_walk_ipaddr(%s, %s) "
1594 			    "icfg_get_index() failed: %s", intf->if_name,
1595 			    af_str(intf->if_protocol), strerror(errno));
1596 		}
1597 		goto done;
1598 	}
1599 
1600 	if (icfg_get_addr(ih, (struct sockaddr *)&addr, &addrlen,
1601 	    &prefixlen, _B_FALSE) != ICFG_SUCCESS) {
1602 		if (errno != ENXIO && errno != ENOENT) {
1603 			vrrp_log(VRRP_ERR, "vrrpd_walk_ipaddr(%s, %s) "
1604 			    "icfg_get_addr() failed: %s", intf->if_name,
1605 			    af_str(intf->if_protocol), strerror(errno));
1606 		}
1607 		goto done;
1608 	}
1609 
1610 	/*
1611 	 * Filter out the all-zero IP address.
1612 	 */
1613 	if (VRRPADDR_UNSPECIFIED(intf->if_protocol, &addr))
1614 		goto done;
1615 
1616 	/*
1617 	 * The interface is unplumbed/replumbed during we walk the IP
1618 	 * addresses. Try walk the IP addresses one more time.
1619 	 */
1620 	if (vrrpd_add_ipaddr(intf->if_name, intf->if_protocol,
1621 	    &addr, ifindex, flags) == VRRP_EAGAIN)
1622 		err = ICFG_FAILURE;
1623 
1624 done:
1625 	icfg_close(ih);
1626 	return (err);
1627 }
1628 
1629 /*
1630  * Given the information of each IP address, update the interface and
1631  * IP addresses list
1632  */
1633 static vrrp_err_t
1634 vrrpd_add_ipaddr(char *lifname, int af, vrrp_addr_t *addr, int ifindex,
1635     uint64_t flags)
1636 {
1637 	char		ifname[LIFNAMSIZ], *c;
1638 	vrrp_intf_t	*intf;
1639 	vrrp_ip_t	*ip;
1640 	char		abuf[INET6_ADDRSTRLEN];
1641 	vrrp_err_t	err;
1642 
1643 	/* LINTED E_CONSTANT_CONDITION */
1644 	VRRPADDR2STR(af, addr, abuf, INET6_ADDRSTRLEN, _B_FALSE);
1645 	vrrp_log(VRRP_DBG0, "vrrpd_add_ipaddr(%s, %s, %d, 0x%x)", lifname,
1646 	    abuf, ifindex, flags);
1647 
1648 	/*
1649 	 * Get the physical interface name from the logical interface name.
1650 	 */
1651 	(void) strlcpy(ifname, lifname, sizeof (ifname));
1652 	if ((c = strchr(ifname, ':')) != NULL)
1653 		*c = '\0';
1654 
1655 	if ((intf = vrrpd_lookup_if(ifname, af)) == NULL) {
1656 		vrrp_log(VRRP_DBG0, "vrrpd_add_ipaddr(): %s is new", ifname);
1657 		err = vrrpd_create_if(ifname, af, ifindex, &intf);
1658 		if (err != VRRP_SUCCESS)
1659 			return (err);
1660 	} else if (intf->vvi_ifindex != ifindex) {
1661 		/*
1662 		 * If index changes, it means that this interface is
1663 		 * unplumbed/replumbed since we last checked. If this
1664 		 * interface is not used by any VRRP router, just
1665 		 * update its ifindex, and the IP addresses list will
1666 		 * be updated later. Otherwise, return EAGAIN to rewalk
1667 		 * all the IP addresses from the beginning.
1668 		 */
1669 		vrrp_log(VRRP_DBG0, "vrrpd_add_ipaddr(%s) ifindex changed ",
1670 		    "from %d to %d", ifname, intf->vvi_ifindex, ifindex);
1671 		if (!IS_PRIMARY_INTF(intf) && !IS_VIRTUAL_INTF(intf)) {
1672 			intf->vvi_ifindex = ifindex;
1673 		} else {
1674 			/*
1675 			 * delete this interface from the list if this
1676 			 * interface has already been assoicated with
1677 			 * any VRRP routers.
1678 			 */
1679 			vrrpd_delete_if(intf, _B_TRUE);
1680 			return (VRRP_EAGAIN);
1681 		}
1682 	}
1683 
1684 	/*
1685 	 * Does this IP address already exist?
1686 	 */
1687 	TAILQ_FOREACH(ip, &intf->vvi_iplist, vip_next) {
1688 		if (strcmp(ip->vip_lifname, lifname) == 0)
1689 			break;
1690 	}
1691 
1692 	if (ip != NULL) {
1693 		vrrp_log(VRRP_DBG0, "vrrpd_add_ipaddr(%s, %s) IP exists",
1694 		    lifname, abuf);
1695 		ip->vip_state = NODE_STATE_NONE;
1696 		ip->vip_flags = flags;
1697 		if (ipaddr_cmp(af, addr, &ip->vip_addr) != 0) {
1698 			/*
1699 			 * Address has been changed, mark it as new
1700 			 * If this address is already selected as the
1701 			 * primary IP address, the new IP will be checked
1702 			 * to see whether it is still qualified as the
1703 			 * primary IP address. If not, the primary IP
1704 			 * address will be reselected.
1705 			 */
1706 			(void) memcpy(&ip->vip_addr, addr,
1707 			    sizeof (vrrp_addr_t));
1708 
1709 			ip->vip_state = NODE_STATE_NEW;
1710 		}
1711 	} else {
1712 		vrrp_log(VRRP_DBG0, "vrrpd_add_ipaddr(%s, %s) IP is new",
1713 		    lifname, abuf);
1714 
1715 		err = vrrpd_create_ip(intf, lifname, addr, flags);
1716 		if (err != VRRP_SUCCESS)
1717 			return (err);
1718 	}
1719 	return (VRRP_SUCCESS);
1720 }
1721 
1722 /*
1723  * Update the interface and IP addresses list. Remove the ones that have been
1724  * staled since last time we walk the IP addresses and updated the ones that
1725  * have been changed.
1726  */
1727 static void
1728 vrrpd_update_ipcache(int af)
1729 {
1730 	vrrp_intf_t	*intf, *nextif;
1731 	vrrp_ip_t	*ip, *nextip;
1732 	char		abuf[INET6_ADDRSTRLEN];
1733 	boolean_t	primary_selected;
1734 	boolean_t	primary_now_selected;
1735 	boolean_t	need_reenable = _B_FALSE;
1736 
1737 	vrrp_log(VRRP_DBG0, "vrrpd_update_ipcache(%s)", af_str(af));
1738 
1739 	nextif = TAILQ_FIRST(&vrrp_intf_list);
1740 	while ((intf = nextif) != NULL) {
1741 		nextif = TAILQ_NEXT(intf, vvi_next);
1742 		if (intf->vvi_af != af)
1743 			continue;
1744 
1745 		/*
1746 		 * Does the interface already select its primary IP address?
1747 		 */
1748 		primary_selected = (intf->vvi_pip != NULL);
1749 		assert(!primary_selected || IS_PRIMARY_INTF(intf));
1750 
1751 		/*
1752 		 * Removed the IP addresses that have been unconfigured.
1753 		 */
1754 		for (ip = TAILQ_FIRST(&intf->vvi_iplist); ip != NULL;
1755 		    ip = nextip) {
1756 			nextip = TAILQ_NEXT(ip, vip_next);
1757 			if (ip->vip_state != NODE_STATE_STALE)
1758 				continue;
1759 
1760 			/* LINTED E_CONSTANT_CONDITION */
1761 			VRRPADDR2STR(af, &ip->vip_addr, abuf, INET6_ADDRSTRLEN,
1762 			    _B_FALSE);
1763 			vrrp_log(VRRP_DBG0, "vrrpd_update_ipcache(): IP %s "
1764 			    "is removed over %s", abuf, intf->vvi_ifname);
1765 			vrrpd_delete_ip(intf, ip);
1766 		}
1767 
1768 		/*
1769 		 * No IP addresses left, delete this interface.
1770 		 */
1771 		if (TAILQ_EMPTY(&intf->vvi_iplist)) {
1772 			vrrp_log(VRRP_DBG0, "vrrpd_update_ipcache(): "
1773 			    "no IP left over %s", intf->vvi_ifname);
1774 			vrrpd_delete_if(intf, _B_TRUE);
1775 			continue;
1776 		}
1777 
1778 		/*
1779 		 * If this is selected ss the physical interface for any
1780 		 * VRRP router, reselect the primary address if needed.
1781 		 */
1782 		if (IS_PRIMARY_INTF(intf)) {
1783 			vrrpd_reselect_primary(intf);
1784 			primary_now_selected = (intf->vvi_pip != NULL);
1785 
1786 			/*
1787 			 * Cannot find the new primary IP address.
1788 			 */
1789 			if (primary_selected && !primary_now_selected) {
1790 				vrrp_log(VRRP_DBG0, "vrrpd_update_ipcache() "
1791 				    "reselect primary IP on %s failed",
1792 				    intf->vvi_ifname);
1793 				vrrpd_remove_if(intf, _B_TRUE);
1794 			} else if (!primary_selected && primary_now_selected) {
1795 				/*
1796 				 * The primary IP address is successfully
1797 				 * selected on the physical interfacew we
1798 				 * need to walk through all the VRRP routers
1799 				 * that is created on this physical interface
1800 				 * and see whether they can now be enabled.
1801 				 */
1802 				need_reenable = _B_TRUE;
1803 			}
1804 		}
1805 
1806 		/*
1807 		 * For every new virtual IP address, bring up/down it based
1808 		 * on the state of VRRP router.
1809 		 *
1810 		 * Note that it is fine to not update the IP's vip_flags field
1811 		 * even if vrrpd_virtualip_updateone() changed the address's
1812 		 * up/down state, since the vip_flags field is only used for
1813 		 * select primary IP address over a physical interface, and
1814 		 * vrrpd_virtualip_updateone() only affects the virtual IP
1815 		 * address's status.
1816 		 */
1817 		for (ip = TAILQ_FIRST(&intf->vvi_iplist); ip != NULL;
1818 		    ip = nextip) {
1819 			nextip = TAILQ_NEXT(ip, vip_next);
1820 			/* LINTED E_CONSTANT_CONDITION */
1821 			VRRPADDR2STR(af, &ip->vip_addr, abuf, INET6_ADDRSTRLEN,
1822 			    _B_FALSE);
1823 			vrrp_log(VRRP_DBG0, "vrrpd_update_ipcache(): "
1824 			    "IP %s over %s%s", abuf, intf->vvi_ifname,
1825 			    ip->vip_state == NODE_STATE_NEW ? " is new" : "");
1826 
1827 			if (IS_VIRTUAL_INTF(intf)) {
1828 				/*
1829 				 * If this IP is new, update its up/down state
1830 				 * based on the virtual interface's state
1831 				 * (which is determined by the VRRP router's
1832 				 * state). Otherwise, check only and prompt
1833 				 * warnings if its up/down state has been
1834 				 * changed.
1835 				 */
1836 				if (vrrpd_virtualip_updateone(intf, ip,
1837 				    ip->vip_state == NODE_STATE_NONE) !=
1838 				    VRRP_SUCCESS) {
1839 					vrrp_log(VRRP_DBG0,
1840 					    "vrrpd_update_ipcache(): "
1841 					    "IP %s over %s update failed", abuf,
1842 					    intf->vvi_ifname);
1843 					vrrpd_delete_ip(intf, ip);
1844 					continue;
1845 				}
1846 			}
1847 			ip->vip_state = NODE_STATE_NONE;
1848 		}
1849 
1850 		/*
1851 		 * The IP address is deleted when it is failed to be brought
1852 		 * up. If no IP addresses are left, delete this interface.
1853 		 */
1854 		if (TAILQ_EMPTY(&intf->vvi_iplist)) {
1855 			vrrp_log(VRRP_DBG0, "vrrpd_update_ipcache(): "
1856 			    "no IP left over %s", intf->vvi_ifname);
1857 			vrrpd_delete_if(intf, _B_TRUE);
1858 			continue;
1859 		}
1860 
1861 		if (intf->vvi_state == NODE_STATE_NEW) {
1862 			/*
1863 			 * A new interface is found. This interface can be
1864 			 * the primary interface or the virtual VNIC
1865 			 * interface.  Again, we need to walk throught all
1866 			 * the VRRP routers to see whether some of them can
1867 			 * now be enabled because of the new primary IP
1868 			 * address or the new virtual IP addresses.
1869 			 */
1870 			intf->vvi_state = NODE_STATE_NONE;
1871 			need_reenable = _B_TRUE;
1872 		}
1873 	}
1874 
1875 	if (need_reenable)
1876 		vrrpd_reenable_all_vr();
1877 }
1878 
1879 /*
1880  * Reselect primary IP if:
1881  * - The existing primary IP is no longer qualified (removed or it is down or
1882  *   not a link-local IP for IPv6 VRRP router);
1883  * - This is a physical interface but no primary IP is chosen;
1884  */
1885 static void
1886 vrrpd_reselect_primary(vrrp_intf_t *intf)
1887 {
1888 	vrrp_ip_t	*ip;
1889 	char		abuf[INET6_ADDRSTRLEN];
1890 
1891 	assert(IS_PRIMARY_INTF(intf));
1892 
1893 	/*
1894 	 * If the interface's old primary IP address is still valid, return
1895 	 */
1896 	if (((ip = intf->vvi_pip) != NULL) && (QUALIFY_PRIMARY_ADDR(intf, ip)))
1897 		return;
1898 
1899 	if (ip != NULL) {
1900 		/* LINTED E_CONSTANT_CONDITION */
1901 		VRRPADDR2STR(intf->vvi_af, &ip->vip_addr, abuf,
1902 		    sizeof (abuf), _B_FALSE);
1903 		vrrp_log(VRRP_DBG0, "vrrpd_reselect_primary(%s): primary IP %s "
1904 		    "is no longer qualified", intf->vvi_ifname, abuf);
1905 	}
1906 
1907 	ip = vrrpd_select_primary(intf);
1908 	intf->vvi_pip = ip;
1909 
1910 	if (ip != NULL) {
1911 		/* LINTED E_CONSTANT_CONDITION */
1912 		VRRPADDR2STR(intf->vvi_af, &ip->vip_addr, abuf,
1913 		    sizeof (abuf), _B_FALSE);
1914 		vrrp_log(VRRP_DBG0, "vrrpd_reselect_primary(%s): primary IP %s "
1915 		    "is selected", intf->vvi_ifname, abuf);
1916 	}
1917 }
1918 
1919 /*
1920  * Select the primary IP address. Since the link-local IP address is always
1921  * at the head of the IP address list, try to find the first UP IP address
1922  * and see whether it qualify.
1923  */
1924 static vrrp_ip_t *
1925 vrrpd_select_primary(vrrp_intf_t *pif)
1926 {
1927 	vrrp_ip_t	*pip;
1928 	char		abuf[INET6_ADDRSTRLEN];
1929 
1930 	vrrp_log(VRRP_DBG1, "vrrpd_select_primary(%s)", pif->vvi_ifname);
1931 
1932 	TAILQ_FOREACH(pip, &pif->vvi_iplist, vip_next) {
1933 		assert(pip->vip_state != NODE_STATE_STALE);
1934 
1935 		/* LINTED E_CONSTANT_CONDITION */
1936 		VRRPADDR2STR(pif->vvi_af, &pip->vip_addr, abuf,
1937 		    INET6_ADDRSTRLEN, _B_FALSE);
1938 		vrrp_log(VRRP_DBG0, "vrrpd_select_primary(%s): %s is %s",
1939 		    pif->vvi_ifname, abuf,
1940 		    (pip->vip_flags & IFF_UP) ? "up" : "down");
1941 
1942 		if (pip->vip_flags & IFF_UP)
1943 			break;
1944 	}
1945 
1946 	/*
1947 	 * Is this valid primary IP address?
1948 	 */
1949 	if (pip == NULL || !QUALIFY_PRIMARY_ADDR(pif, pip)) {
1950 		vrrp_log(VRRP_DBG0, "vrrpd_select_primary(%s/%s) failed",
1951 		    pif->vvi_ifname, af_str(pif->vvi_af));
1952 		return (NULL);
1953 	}
1954 	return (pip);
1955 }
1956 
1957 /*
1958  * This is a new interface. Check whether any VRRP router is waiting for it
1959  */
1960 static void
1961 vrrpd_reenable_all_vr()
1962 {
1963 	vrrp_vr_t *vr;
1964 
1965 	vrrp_log(VRRP_DBG0, "vrrpd_reenable_all_vr()");
1966 
1967 	TAILQ_FOREACH(vr, &vrrp_vr_list, vvr_next) {
1968 		if (vr->vvr_conf.vvc_enabled)
1969 			(void) vrrpd_enable_vr(vr);
1970 	}
1971 }
1972 
1973 /*
1974  * If primary_addr_gone is _B_TRUE, it means that we failed to select
1975  * the primary IP address on this (physical) interface; otherwise,
1976  * it means the interface is no longer available.
1977  */
1978 static void
1979 vrrpd_remove_if(vrrp_intf_t *intf, boolean_t primary_addr_gone)
1980 {
1981 	vrrp_vr_t *vr;
1982 
1983 	vrrp_log(VRRP_DBG0, "vrrpd_remove_if(%s): %s", intf->vvi_ifname,
1984 	    primary_addr_gone ? "primary address gone" : "interface deleted");
1985 
1986 	TAILQ_FOREACH(vr, &vrrp_vr_list, vvr_next) {
1987 		if (vr->vvr_conf.vvc_enabled)
1988 			vrrpd_disable_vr(vr, intf, primary_addr_gone);
1989 	}
1990 }
1991 
1992 /*
1993  * Update the VRRP configuration file based on the given configuration.
1994  * op is either VRRP_CONF_UPDATE or VRRP_CONF_DELETE
1995  */
1996 static vrrp_err_t
1997 vrrpd_updateconf(vrrp_vr_conf_t *newconf, uint_t op)
1998 {
1999 	vrrp_vr_conf_t	conf;
2000 	FILE		*fp, *nfp;
2001 	int		nfd;
2002 	char		line[LINE_MAX];
2003 	char		newfile[MAXPATHLEN];
2004 	boolean_t	found = _B_FALSE;
2005 	vrrp_err_t	err = VRRP_SUCCESS;
2006 
2007 	vrrp_log(VRRP_DBG0, "vrrpd_updateconf(%s, %s)", newconf->vvc_name,
2008 	    op == VRRP_CONF_UPDATE ? "update" : "delete");
2009 
2010 	if ((fp = fopen(vrrpd_conffile, "r+F")) == NULL) {
2011 		vrrp_log(VRRP_ERR, "vrrpd_updateconf(): open %s failed: %s",
2012 		    vrrpd_conffile, strerror(errno));
2013 		return (VRRP_EDB);
2014 	}
2015 
2016 	(void) snprintf(newfile, MAXPATHLEN, "%s.new", vrrpd_conffile);
2017 	if ((nfd = open(newfile, O_WRONLY | O_CREAT | O_TRUNC,
2018 	    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0) {
2019 		vrrp_log(VRRP_ERR, "vrrpd_updateconf(): open %s failed: %s",
2020 		    newfile, strerror(errno));
2021 		(void) fclose(fp);
2022 		return (VRRP_EDB);
2023 	}
2024 
2025 	if ((nfp = fdopen(nfd, "wF")) == NULL) {
2026 		vrrp_log(VRRP_ERR, "vrrpd_updateconf(): fdopen(%s) failed: %s",
2027 		    newfile, strerror(errno));
2028 		goto done;
2029 	}
2030 
2031 	while (fgets(line, sizeof (line), fp) != NULL) {
2032 		conf.vvc_vrid = VRRP_VRID_NONE;
2033 		if (!found && (err = vrrpd_read_vrconf(line, &conf)) !=
2034 		    VRRP_SUCCESS) {
2035 			vrrp_log(VRRP_ERR, "vrrpd_updateconf(): invalid "
2036 			    "configuration format: %s", line);
2037 			goto done;
2038 		}
2039 
2040 		/*
2041 		 * Write this line out if:
2042 		 * - this is a comment line; or
2043 		 * - we've done updating/deleting the the given VR; or
2044 		 * - if the name of the VR read from this line does not match
2045 		 *   the VR name that we are about to update/delete;
2046 		 */
2047 		if (found || conf.vvc_vrid == VRRP_VRID_NONE ||
2048 		    strcmp(conf.vvc_name, newconf->vvc_name) != 0) {
2049 			if (fputs(line, nfp) != EOF)
2050 				continue;
2051 
2052 			vrrp_log(VRRP_ERR, "vrrpd_updateconf(): failed to "
2053 			    "write line %s", line);
2054 			err = VRRP_EDB;
2055 			goto done;
2056 		}
2057 
2058 		/*
2059 		 * Otherwise, update/skip the line.
2060 		 */
2061 		found = _B_TRUE;
2062 		if (op == VRRP_CONF_DELETE)
2063 			continue;
2064 
2065 		assert(op == VRRP_CONF_UPDATE);
2066 		if ((err = vrrpd_write_vrconf(line, sizeof (line),
2067 		    newconf)) != VRRP_SUCCESS) {
2068 			vrrp_log(VRRP_ERR, "vrrpd_updateconf(): failed to "
2069 			    "update configuration for %s", newconf->vvc_name);
2070 			goto done;
2071 		}
2072 		if (fputs(line, nfp) == EOF) {
2073 			vrrp_log(VRRP_ERR, "vrrpd_updateconf(): failed to "
2074 			    "write line %s", line);
2075 			err = VRRP_EDB;
2076 			goto done;
2077 		}
2078 	}
2079 
2080 	/*
2081 	 * If we get to the end of the file and have not seen the router that
2082 	 * we are about to update, write it out.
2083 	 */
2084 	if (!found && op == VRRP_CONF_UPDATE) {
2085 		if ((err = vrrpd_write_vrconf(line, sizeof (line),
2086 		    newconf)) == VRRP_SUCCESS && fputs(line, nfp) == EOF) {
2087 			vrrp_log(VRRP_ERR, "vrrpd_updateconf(): failed to "
2088 			    "write line %s", line);
2089 			err = VRRP_EDB;
2090 		}
2091 	} else if (!found && op == VRRP_CONF_DELETE) {
2092 		vrrp_log(VRRP_ERR, "vrrpd_updateconf(): failed to find "
2093 		    "configuation for %s", newconf->vvc_name);
2094 		err = VRRP_ENOTFOUND;
2095 	}
2096 
2097 	if (err != VRRP_SUCCESS)
2098 		goto done;
2099 
2100 	if (fflush(nfp) == EOF || rename(newfile, vrrpd_conffile) < 0) {
2101 		vrrp_log(VRRP_ERR, "vrrpd_updateconf(): failed to "
2102 		    "rename file %s", newfile);
2103 		err = VRRP_EDB;
2104 	}
2105 
2106 done:
2107 	(void) fclose(fp);
2108 	(void) fclose(nfp);
2109 	(void) unlink(newfile);
2110 	return (err);
2111 }
2112 
2113 static vrrp_err_t
2114 vrrpd_write_vrconf(char *line, size_t len, vrrp_vr_conf_t *conf)
2115 {
2116 	vrrp_prop_t	*prop;
2117 	int		n, i;
2118 
2119 	vrrp_log(VRRP_DBG0, "vrrpd_write_vrconf(%s)", conf->vvc_name);
2120 
2121 	for (i = 0; i < VRRP_PROP_INFO_TABSIZE; i++) {
2122 		prop = &vrrp_prop_info_tbl[i];
2123 		n = snprintf(line, len, i == 0 ? "%s=" : " %s=",
2124 		    prop->vs_propname);
2125 		if (n < 0 || n >= len)
2126 			break;
2127 		len -= n;
2128 		line += n;
2129 		n = prop->vs_propwrite(conf, line, len);
2130 		if (n < 0 || n >= len)
2131 			break;
2132 		len -= n;
2133 		line += n;
2134 	}
2135 	if (i != VRRP_PROP_INFO_TABSIZE) {
2136 		vrrp_log(VRRP_ERR, "vrrpd_write_vrconf(%s): buffer size too"
2137 		    "small", conf->vvc_name);
2138 		return (VRRP_EDB);
2139 	}
2140 	n = snprintf(line, len, "\n");
2141 	if (n < 0 || n >= len) {
2142 		vrrp_log(VRRP_ERR, "vrrpd_write_vrconf(%s): buffer size too"
2143 		    "small", conf->vvc_name);
2144 		return (VRRP_EDB);
2145 	}
2146 	return (VRRP_SUCCESS);
2147 }
2148 
2149 static vrrp_err_t
2150 vrrpd_read_vrconf(char *line, vrrp_vr_conf_t *conf)
2151 {
2152 	char		*str, *token;
2153 	char		*next;
2154 	vrrp_err_t	err = VRRP_SUCCESS;
2155 	char		tmpbuf[MAXLINELEN];
2156 
2157 	str = tmpbuf;
2158 	(void) strlcpy(tmpbuf, line, MAXLINELEN);
2159 
2160 	/*
2161 	 * Skip leading spaces, blank lines, and comments.
2162 	 */
2163 	skip_whitespace(str);
2164 	if ((str - tmpbuf == strlen(tmpbuf)) || (*str == '#')) {
2165 		conf->vvc_vrid = VRRP_VRID_NONE;
2166 		return (VRRP_SUCCESS);
2167 	}
2168 
2169 	/*
2170 	 * Read each VR properties.
2171 	 */
2172 	for (token = strtok_r(str, " \n\t", &next); token != NULL;
2173 	    token = strtok_r(NULL, " \n\t", &next)) {
2174 		if ((err = vrrpd_readprop(token, conf)) != VRRP_SUCCESS)
2175 			break;
2176 	}
2177 
2178 	/* All properties read but no VRID defined */
2179 	if (err == VRRP_SUCCESS && conf->vvc_vrid == VRRP_VRID_NONE)
2180 		err = VRRP_EINVAL;
2181 
2182 	return (err);
2183 }
2184 
2185 static vrrp_err_t
2186 vrrpd_readprop(const char *str, vrrp_vr_conf_t *conf)
2187 {
2188 	vrrp_prop_t	*prop;
2189 	char		*pstr;
2190 	int		i;
2191 
2192 	if ((pstr = strchr(str, '=')) == NULL) {
2193 		vrrp_log(VRRP_ERR, "vrrpd_readprop(%s): invalid property", str);
2194 		return (VRRP_EINVAL);
2195 	}
2196 
2197 	*pstr++ = '\0';
2198 	for (i = 0; i < VRRP_PROP_INFO_TABSIZE; i++) {
2199 		prop = &vrrp_prop_info_tbl[i];
2200 		if (strcasecmp(str, prop->vs_propname) == 0) {
2201 			if (prop->vs_propread(conf, pstr))
2202 				break;
2203 		}
2204 	}
2205 
2206 	if (i == VRRP_PROP_INFO_TABSIZE) {
2207 		vrrp_log(VRRP_ERR, "vrrpd_readprop(%s): invalid property", str);
2208 		return (VRRP_EINVAL);
2209 	}
2210 
2211 	return (VRRP_SUCCESS);
2212 }
2213 
2214 static boolean_t
2215 vrrp_rd_prop_name(vrrp_vr_conf_t *conf, const char *str)
2216 {
2217 	size_t size = sizeof (conf->vvc_name);
2218 	return (strlcpy(conf->vvc_name, str, size) < size);
2219 }
2220 
2221 static boolean_t
2222 vrrp_rd_prop_vrid(vrrp_vr_conf_t *conf, const char *str)
2223 {
2224 	conf->vvc_vrid = strtol(str, NULL, 0);
2225 	return (!(conf->vvc_vrid < VRRP_VRID_MIN ||
2226 	    conf->vvc_vrid > VRRP_VRID_MAX ||
2227 	    (conf->vvc_vrid == 0 && errno != 0)));
2228 }
2229 
2230 static boolean_t
2231 vrrp_rd_prop_af(vrrp_vr_conf_t *conf, const char *str)
2232 {
2233 	if (strcasecmp(str, "AF_INET") == 0)
2234 		conf->vvc_af = AF_INET;
2235 	else if (strcasecmp(str, "AF_INET6") == 0)
2236 		conf->vvc_af = AF_INET6;
2237 	else
2238 		return (_B_FALSE);
2239 	return (_B_TRUE);
2240 }
2241 
2242 static boolean_t
2243 vrrp_rd_prop_pri(vrrp_vr_conf_t *conf, const char *str)
2244 {
2245 	conf->vvc_pri = strtol(str, NULL, 0);
2246 	return (!(conf->vvc_pri < VRRP_PRI_MIN ||
2247 	    conf->vvc_pri > VRRP_PRI_OWNER ||
2248 	    (conf->vvc_pri == 0 && errno != 0)));
2249 }
2250 
2251 static boolean_t
2252 vrrp_rd_prop_adver_int(vrrp_vr_conf_t *conf, const char *str)
2253 {
2254 	conf->vvc_adver_int = strtol(str, NULL, 0);
2255 	return (!(conf->vvc_adver_int < VRRP_MAX_ADVER_INT_MIN ||
2256 	    conf->vvc_adver_int > VRRP_MAX_ADVER_INT_MAX ||
2257 	    (conf->vvc_adver_int == 0 && errno != 0)));
2258 }
2259 
2260 static boolean_t
2261 vrrp_rd_prop_preempt(vrrp_vr_conf_t *conf, const char *str)
2262 {
2263 	if (strcasecmp(str, "true") == 0)
2264 		conf->vvc_preempt = _B_TRUE;
2265 	else if (strcasecmp(str, "false") == 0)
2266 		conf->vvc_preempt = _B_FALSE;
2267 	else
2268 		return (_B_FALSE);
2269 	return (_B_TRUE);
2270 }
2271 
2272 static boolean_t
2273 vrrp_rd_prop_accept(vrrp_vr_conf_t *conf, const char *str)
2274 {
2275 	if (strcasecmp(str, "true") == 0)
2276 		conf->vvc_accept = _B_TRUE;
2277 	else if (strcasecmp(str, "false") == 0)
2278 		conf->vvc_accept = _B_FALSE;
2279 	else
2280 		return (_B_FALSE);
2281 	return (_B_TRUE);
2282 }
2283 
2284 static boolean_t
2285 vrrp_rd_prop_enabled(vrrp_vr_conf_t *conf, const char *str)
2286 {
2287 	if (strcasecmp(str, "enabled") == 0)
2288 		conf->vvc_enabled = _B_TRUE;
2289 	else if (strcasecmp(str, "disabled") == 0)
2290 		conf->vvc_enabled = _B_FALSE;
2291 	else
2292 		return (_B_FALSE);
2293 	return (_B_TRUE);
2294 }
2295 
2296 static boolean_t
2297 vrrp_rd_prop_ifname(vrrp_vr_conf_t *conf, const char *str)
2298 {
2299 	size_t size = sizeof (conf->vvc_link);
2300 	return (strlcpy(conf->vvc_link, str, size) < size);
2301 }
2302 
2303 static int
2304 vrrp_wt_prop_name(vrrp_vr_conf_t *conf, char *str, size_t size)
2305 {
2306 	return (snprintf(str, size, "%s", conf->vvc_name));
2307 }
2308 
2309 static int
2310 vrrp_wt_prop_pri(vrrp_vr_conf_t *conf, char *str, size_t size)
2311 {
2312 	return (snprintf(str, size, "%d", conf->vvc_pri));
2313 }
2314 
2315 static int
2316 vrrp_wt_prop_adver_int(vrrp_vr_conf_t *conf, char *str, size_t size)
2317 {
2318 	return (snprintf(str, size, "%d", conf->vvc_adver_int));
2319 }
2320 
2321 static int
2322 vrrp_wt_prop_preempt(vrrp_vr_conf_t *conf, char *str, size_t size)
2323 {
2324 	return (snprintf(str, size, "%s",
2325 	    conf->vvc_preempt ? "true" : "false"));
2326 }
2327 
2328 static int
2329 vrrp_wt_prop_accept(vrrp_vr_conf_t *conf, char *str, size_t size)
2330 {
2331 	return (snprintf(str, size, "%s",
2332 	    conf->vvc_accept ? "true" : "false"));
2333 }
2334 
2335 static int
2336 vrrp_wt_prop_enabled(vrrp_vr_conf_t *conf, char *str, size_t size)
2337 {
2338 	return (snprintf(str, size, "%s",
2339 	    conf->vvc_enabled ? "enabled" : "disabled"));
2340 }
2341 
2342 static int
2343 vrrp_wt_prop_vrid(vrrp_vr_conf_t *conf, char *str, size_t size)
2344 {
2345 	return (snprintf(str, size, "%d", conf->vvc_vrid));
2346 }
2347 
2348 static int
2349 vrrp_wt_prop_af(vrrp_vr_conf_t *conf, char *str, size_t size)
2350 {
2351 	return (snprintf(str, size, "%s",
2352 	    conf->vvc_af == AF_INET ? "AF_INET" : "AF_INET6"));
2353 }
2354 
2355 static int
2356 vrrp_wt_prop_ifname(vrrp_vr_conf_t *conf, char *str, size_t size)
2357 {
2358 	return (snprintf(str, size, "%s", conf->vvc_link));
2359 }
2360 
2361 static char *
2362 af_str(int af)
2363 {
2364 	if (af == 4 || af == AF_INET)
2365 		return ("AF_INET");
2366 	else if (af == 6 || af == AF_INET6)
2367 		return ("AF_INET6");
2368 	else if (af == AF_UNSPEC)
2369 		return ("AF_UNSPEC");
2370 	else
2371 		return ("AF_error");
2372 }
2373 
2374 static vrrp_err_t
2375 vrrpd_create_vr(vrrp_vr_conf_t *conf)
2376 {
2377 	vrrp_vr_t	*vr;
2378 
2379 	vrrp_log(VRRP_DBG0, "vrrpd_create_vr(%s)", conf->vvc_name);
2380 
2381 	if ((vr = malloc(sizeof (vrrp_vr_t))) == NULL) {
2382 		vrrp_log(VRRP_ERR, "vrrpd_create_vr(): memory allocation for %s"
2383 		    " failed", conf->vvc_name);
2384 		return (VRRP_ENOMEM);
2385 	}
2386 
2387 	bzero(vr, sizeof (vrrp_vr_t));
2388 	vr->vvr_state = VRRP_STATE_NONE;
2389 	vr->vvr_timer_id = -1;
2390 	vrrpd_state_trans(VRRP_STATE_NONE, VRRP_STATE_INIT, vr);
2391 	(void) memcpy(&vr->vvr_conf, conf, sizeof (vrrp_vr_conf_t));
2392 	vr->vvr_conf.vvc_enabled = _B_FALSE;
2393 	TAILQ_INSERT_HEAD(&vrrp_vr_list, vr, vvr_next);
2394 	return (VRRP_SUCCESS);
2395 }
2396 
2397 static void
2398 vrrpd_delete_vr(vrrp_vr_t *vr)
2399 {
2400 	vrrp_log(VRRP_DBG0, "vrrpd_delete_vr(%s)", vr->vvr_conf.vvc_name);
2401 	if (vr->vvr_conf.vvc_enabled)
2402 		vrrpd_disable_vr(vr, NULL, _B_FALSE);
2403 	assert(vr->vvr_state == VRRP_STATE_INIT);
2404 	vrrpd_state_trans(VRRP_STATE_INIT, VRRP_STATE_NONE, vr);
2405 	TAILQ_REMOVE(&vrrp_vr_list, vr, vvr_next);
2406 	(void) free(vr);
2407 }
2408 
2409 static vrrp_err_t
2410 vrrpd_enable_vr(vrrp_vr_t *vr)
2411 {
2412 	vrrp_err_t	rx_err, tx_err, err = VRRP_EINVAL;
2413 
2414 	vrrp_log(VRRP_DBG0, "vrrpd_enable_vr(%s)", vr->vvr_conf.vvc_name);
2415 
2416 	assert(vr->vvr_conf.vvc_enabled);
2417 
2418 	/*
2419 	 * This VRRP router has been successfully enabled and start
2420 	 * participating.
2421 	 */
2422 	if (vr->vvr_state != VRRP_STATE_INIT)
2423 		return (VRRP_SUCCESS);
2424 
2425 	if ((rx_err = vrrpd_init_rxsock(vr)) == VRRP_SUCCESS) {
2426 		/*
2427 		 * Select the primary IP address. Even if this time
2428 		 * primary IP selection failed, we will reselect the
2429 		 * primary IP address when new IP address comes up.
2430 		 */
2431 		vrrpd_reselect_primary(vr->vvr_pif);
2432 		if (vr->vvr_pif->vvi_pip == NULL) {
2433 			vrrp_log(VRRP_DBG0, "vrrpd_enable_vr(%s): "
2434 			    "select_primary over %s failed",
2435 			    vr->vvr_conf.vvc_name, vr->vvr_pif->vvi_ifname);
2436 			rx_err = VRRP_ENOPRIM;
2437 		}
2438 	}
2439 
2440 	/*
2441 	 * Initialize the TX socket used for this vrrp_vr_t to send the
2442 	 * multicast packets.
2443 	 */
2444 	tx_err = vrrpd_init_txsock(vr);
2445 
2446 	/*
2447 	 * Only start the state transition if sockets for both RX and TX are
2448 	 * initialized correctly.
2449 	 */
2450 	if (rx_err != VRRP_SUCCESS || tx_err != VRRP_SUCCESS) {
2451 		/*
2452 		 * Record the error information for diagnose purpose.
2453 		 */
2454 		vr->vvr_err = (rx_err == VRRP_SUCCESS) ? tx_err : rx_err;
2455 		return (err);
2456 	}
2457 
2458 	if (vr->vvr_conf.vvc_pri == 255)
2459 		err = vrrpd_state_i2m(vr);
2460 	else
2461 		err = vrrpd_state_i2b(vr);
2462 
2463 	if (err != VRRP_SUCCESS) {
2464 		vr->vvr_err = err;
2465 		vr->vvr_pif->vvi_pip = NULL;
2466 		vrrpd_fini_txsock(vr);
2467 		vrrpd_fini_rxsock(vr);
2468 	}
2469 	return (err);
2470 }
2471 
2472 /*
2473  * Given the removed interface, see whether the given VRRP router would
2474  * be affected and stop participating the VRRP protocol.
2475  *
2476  * If intf is NULL, VR disabling request is coming from the admin.
2477  */
2478 static void
2479 vrrpd_disable_vr(vrrp_vr_t *vr, vrrp_intf_t *intf, boolean_t primary_addr_gone)
2480 {
2481 	vrrp_log(VRRP_DBG0, "vrrpd_disable_vr(%s): %s%s", vr->vvr_conf.vvc_name,
2482 	    intf == NULL ? "requested by admin" : intf->vvi_ifname,
2483 	    intf == NULL ? "" : (primary_addr_gone ? "primary address gone" :
2484 	    "interface deleted"));
2485 
2486 	/*
2487 	 * An interface is deleted, see whether this interface is the
2488 	 * physical interface or the VNIC of the given VRRP router.
2489 	 * If so, continue to disable the VRRP router.
2490 	 */
2491 	if (!primary_addr_gone && (intf != NULL) && (intf != vr->vvr_pif) &&
2492 	    (intf != vr->vvr_vif)) {
2493 		return;
2494 	}
2495 
2496 	/*
2497 	 * If this is the case that the primary IP address is gone,
2498 	 * and we failed to reselect another primary IP address,
2499 	 * continue to disable the VRRP router.
2500 	 */
2501 	if (primary_addr_gone && intf != vr->vvr_pif)
2502 		return;
2503 
2504 	vrrp_log(VRRP_DBG1, "vrrpd_disable_vr(%s): disabling",
2505 	    vr->vvr_conf.vvc_name);
2506 
2507 	if (vr->vvr_state == VRRP_STATE_MASTER) {
2508 		/*
2509 		 * If this router is disabled by the administrator, send
2510 		 * the zero-priority advertisement to indicate the Master
2511 		 * stops participating VRRP.
2512 		 */
2513 		if (intf == NULL)
2514 			(void) vrrpd_send_adv(vr, _B_TRUE);
2515 
2516 		vrrpd_state_m2i(vr);
2517 	} else  if (vr->vvr_state == VRRP_STATE_BACKUP) {
2518 		vrrpd_state_b2i(vr);
2519 	}
2520 
2521 	/*
2522 	 * If no primary IP address can be selected, the VRRP router
2523 	 * stays at the INIT state and will become BACKUP and MASTER when
2524 	 * a primary IP address is reselected.
2525 	 */
2526 	if (primary_addr_gone) {
2527 		vrrp_log(VRRP_DBG1, "vrrpd_disable_vr(%s): primary IP "
2528 		    "is removed", vr->vvr_conf.vvc_name);
2529 		vr->vvr_err = VRRP_ENOPRIM;
2530 	} else if (intf == NULL) {
2531 		/*
2532 		 * The VRRP router is disable by the administrator
2533 		 */
2534 		vrrp_log(VRRP_DBG1, "vrrpd_disable_vr(%s): disabled by admin",
2535 		    vr->vvr_conf.vvc_name);
2536 		vr->vvr_err = VRRP_SUCCESS;
2537 		vrrpd_fini_txsock(vr);
2538 		vrrpd_fini_rxsock(vr);
2539 	} else if (intf == vr->vvr_pif) {
2540 		vrrp_log(VRRP_DBG1, "vrrpd_disable_vr(%s): physical interface "
2541 		    "%s removed", vr->vvr_conf.vvc_name, intf->vvi_ifname);
2542 		vr->vvr_err = VRRP_ENOPRIM;
2543 		vrrpd_fini_rxsock(vr);
2544 	} else if (intf == vr->vvr_vif) {
2545 		vrrp_log(VRRP_DBG1, "vrrpd_disable_vr(%s): VNIC interface %s"
2546 		    " removed", vr->vvr_conf.vvc_name, intf->vvi_ifname);
2547 		vr->vvr_err = VRRP_ENOVIRT;
2548 		vrrpd_fini_txsock(vr);
2549 	}
2550 }
2551 
2552 vrrp_err_t
2553 vrrpd_create(vrrp_vr_conf_t *conf, boolean_t updateconf)
2554 {
2555 	vrrp_err_t	err = VRRP_SUCCESS;
2556 
2557 	vrrp_log(VRRP_DBG0, "vrrpd_create(%s, %s, %d)", conf->vvc_name,
2558 	    conf->vvc_link, conf->vvc_vrid);
2559 
2560 	assert(conf != NULL);
2561 
2562 	/*
2563 	 * Sanity check
2564 	 */
2565 	if ((strlen(conf->vvc_name) == 0) ||
2566 	    (strlen(conf->vvc_link) == 0) ||
2567 	    (conf->vvc_vrid < VRRP_VRID_MIN ||
2568 	    conf->vvc_vrid > VRRP_VRID_MAX) ||
2569 	    (conf->vvc_pri < VRRP_PRI_MIN ||
2570 	    conf->vvc_pri > VRRP_PRI_OWNER) ||
2571 	    (conf->vvc_adver_int < VRRP_MAX_ADVER_INT_MIN ||
2572 	    conf->vvc_adver_int > VRRP_MAX_ADVER_INT_MAX) ||
2573 	    (conf->vvc_af != AF_INET && conf->vvc_af != AF_INET6) ||
2574 	    (conf->vvc_pri == VRRP_PRI_OWNER && !conf->vvc_accept)) {
2575 		vrrp_log(VRRP_DBG1, "vrrpd_create(%s): invalid argument",
2576 		    conf->vvc_name);
2577 		return (VRRP_EINVAL);
2578 	}
2579 
2580 	if (!vrrp_valid_name(conf->vvc_name)) {
2581 		vrrp_log(VRRP_DBG1, "vrrpd_create(): %s is not a valid router "
2582 		    "name", conf->vvc_name);
2583 		return (VRRP_EINVALVRNAME);
2584 	}
2585 
2586 	if (vrrpd_lookup_vr_by_name(conf->vvc_name) != NULL) {
2587 		vrrp_log(VRRP_DBG1, "vrrpd_create(): %s already exists",
2588 		    conf->vvc_name);
2589 		return (VRRP_EINSTEXIST);
2590 	}
2591 
2592 	if (vrrpd_lookup_vr_by_vrid(conf->vvc_link, conf->vvc_vrid,
2593 	    conf->vvc_af) != NULL) {
2594 		vrrp_log(VRRP_DBG1, "vrrpd_create(): VRID %d/%s over %s "
2595 		    "already exists", conf->vvc_vrid, af_str(conf->vvc_af),
2596 		    conf->vvc_link);
2597 		return (VRRP_EVREXIST);
2598 	}
2599 
2600 	if (updateconf && (err = vrrpd_updateconf(conf,
2601 	    VRRP_CONF_UPDATE)) != VRRP_SUCCESS) {
2602 		vrrp_log(VRRP_ERR, "vrrpd_create(): failed to update "
2603 		    "configuration for %s", conf->vvc_name);
2604 		return (err);
2605 	}
2606 
2607 	err = vrrpd_create_vr(conf);
2608 	if (err != VRRP_SUCCESS && updateconf)
2609 		(void) vrrpd_updateconf(conf, VRRP_CONF_DELETE);
2610 
2611 	return (err);
2612 }
2613 
2614 static vrrp_err_t
2615 vrrpd_delete(const char *vn)
2616 {
2617 	vrrp_vr_t	*vr;
2618 	vrrp_err_t	err;
2619 
2620 	vrrp_log(VRRP_DBG0, "vrrpd_delete(%s)", vn);
2621 
2622 	if ((vr = vrrpd_lookup_vr_by_name(vn)) == NULL) {
2623 		vrrp_log(VRRP_DBG1, "vrrpd_delete(): %s not exists", vn);
2624 		return (VRRP_ENOTFOUND);
2625 	}
2626 
2627 	err = vrrpd_updateconf(&vr->vvr_conf, VRRP_CONF_DELETE);
2628 	if (err != VRRP_SUCCESS) {
2629 		vrrp_log(VRRP_ERR, "vrrpd_delete(): failed to delete "
2630 		    "configuration for %s", vr->vvr_conf.vvc_name);
2631 		return (err);
2632 	}
2633 
2634 	vrrpd_delete_vr(vr);
2635 	return (VRRP_SUCCESS);
2636 }
2637 
2638 static vrrp_err_t
2639 vrrpd_enable(const char *vn, boolean_t updateconf)
2640 {
2641 	vrrp_vr_t		*vr;
2642 	vrrp_vr_conf_t		*conf;
2643 	uint32_t		flags;
2644 	datalink_class_t	class;
2645 	vrrp_err_t		err = VRRP_SUCCESS;
2646 
2647 	vrrp_log(VRRP_DBG0, "vrrpd_enable(%s)", vn);
2648 
2649 	if ((vr = vrrpd_lookup_vr_by_name(vn)) == NULL) {
2650 		vrrp_log(VRRP_DBG1, "vrrpd_enable(): %s does not exist", vn);
2651 		return (VRRP_ENOTFOUND);
2652 	}
2653 
2654 	/*
2655 	 * The VR is already enabled.
2656 	 */
2657 	conf = &vr->vvr_conf;
2658 	if (conf->vvc_enabled) {
2659 		vrrp_log(VRRP_DBG1, "vrrpd_enable(): %s is already "
2660 		    "enabled", vn);
2661 		return (VRRP_EALREADY);
2662 	}
2663 
2664 	/*
2665 	 * Check whether the link exists.
2666 	 */
2667 	if ((strlen(conf->vvc_link) == 0) || dladm_name2info(vrrpd_vh->vh_dh,
2668 	    conf->vvc_link, NULL, &flags, &class, NULL) != DLADM_STATUS_OK ||
2669 	    !(flags & DLADM_OPT_ACTIVE) || ((class != DATALINK_CLASS_PHYS) &&
2670 	    (class != DATALINK_CLASS_VLAN) && (class != DATALINK_CLASS_AGGR))) {
2671 		vrrp_log(VRRP_DBG1, "vrrpd_enable(%s): invalid link %s",
2672 		    vn, conf->vvc_link);
2673 		return (VRRP_EINVALLINK);
2674 	}
2675 
2676 	/*
2677 	 * Get the associated VNIC name by the given interface/vrid/
2678 	 * address famitly.
2679 	 */
2680 	err = vrrp_get_vnicname(vrrpd_vh, conf->vvc_vrid,
2681 	    conf->vvc_af, conf->vvc_link, NULL, NULL, vr->vvr_vnic,
2682 	    sizeof (vr->vvr_vnic));
2683 	if (err != VRRP_SUCCESS) {
2684 		vrrp_log(VRRP_DBG1, "vrrpd_enable(%s): no VNIC for VRID %d/%s "
2685 		    "over %s", vn, conf->vvc_vrid, af_str(conf->vvc_af),
2686 		    conf->vvc_link);
2687 		err = VRRP_ENOVNIC;
2688 		goto fail;
2689 	}
2690 
2691 	/*
2692 	 * Find the right VNIC, primary interface and get the list of the
2693 	 * protected IP adressses and primary IP address. Note that if
2694 	 * either interface is NULL (no IP addresses configured over the
2695 	 * interface), we will still continue and mark this VRRP router
2696 	 * as "enabled".
2697 	 */
2698 	vr->vvr_conf.vvc_enabled = _B_TRUE;
2699 	if (updateconf && (err = vrrpd_updateconf(&vr->vvr_conf,
2700 	    VRRP_CONF_UPDATE)) != VRRP_SUCCESS) {
2701 		vrrp_log(VRRP_ERR, "vrrpd_enable(): failed to update "
2702 		    "configuration for %s", vr->vvr_conf.vvc_name);
2703 		goto fail;
2704 	}
2705 
2706 	/*
2707 	 * If vrrpd_setup_vr() fails, it is possible that there is no IP
2708 	 * addresses over ether the primary interface or the VNIC yet,
2709 	 * return success in this case, the VRRP router will stay in
2710 	 * the initialized state and start to work when the IP address is
2711 	 * configured.
2712 	 */
2713 	(void) vrrpd_enable_vr(vr);
2714 	return (VRRP_SUCCESS);
2715 
2716 fail:
2717 	vr->vvr_conf.vvc_enabled = _B_FALSE;
2718 	vr->vvr_vnic[0] = '\0';
2719 	return (err);
2720 }
2721 
2722 static vrrp_err_t
2723 vrrpd_disable(const char *vn)
2724 {
2725 	vrrp_vr_t	*vr;
2726 	vrrp_err_t	err;
2727 
2728 	vrrp_log(VRRP_DBG0, "vrrpd_disable(%s)", vn);
2729 
2730 	if ((vr = vrrpd_lookup_vr_by_name(vn)) == NULL) {
2731 		vrrp_log(VRRP_DBG1, "vrrpd_disable(): %s does not exist", vn);
2732 		return (VRRP_ENOTFOUND);
2733 	}
2734 
2735 	/*
2736 	 * The VR is already disable.
2737 	 */
2738 	if (!vr->vvr_conf.vvc_enabled) {
2739 		vrrp_log(VRRP_DBG1, "vrrpd_disable(): %s was not enabled", vn);
2740 		return (VRRP_EALREADY);
2741 	}
2742 
2743 	vr->vvr_conf.vvc_enabled = _B_FALSE;
2744 	err = vrrpd_updateconf(&vr->vvr_conf, VRRP_CONF_UPDATE);
2745 	if (err != VRRP_SUCCESS) {
2746 		vr->vvr_conf.vvc_enabled = _B_TRUE;
2747 		vrrp_log(VRRP_ERR, "vrrpd_disable(): failed to update "
2748 		    "configuration for %s", vr->vvr_conf.vvc_name);
2749 		return (err);
2750 	}
2751 
2752 	vrrpd_disable_vr(vr, NULL, _B_FALSE);
2753 	vr->vvr_vnic[0] = '\0';
2754 	return (VRRP_SUCCESS);
2755 }
2756 
2757 static vrrp_err_t
2758 vrrpd_modify(vrrp_vr_conf_t *conf, uint32_t mask)
2759 {
2760 	vrrp_vr_t	*vr;
2761 	vrrp_vr_conf_t	savconf;
2762 	int		pri;
2763 	boolean_t	accept, set_accept = _B_FALSE;
2764 	vrrp_err_t	err;
2765 
2766 	vrrp_log(VRRP_DBG0, "vrrpd_modify(%s)", conf->vvc_name);
2767 
2768 	if (mask == 0)
2769 		return (VRRP_SUCCESS);
2770 
2771 	if ((vr = vrrpd_lookup_vr_by_name(conf->vvc_name)) == NULL) {
2772 		vrrp_log(VRRP_DBG1, "vrrpd_modify(): cannot find the given "
2773 		    "VR instance: %s", conf->vvc_name);
2774 		return (VRRP_ENOTFOUND);
2775 	}
2776 
2777 	if (mask & VRRP_CONF_INTERVAL) {
2778 		if (conf->vvc_adver_int < VRRP_MAX_ADVER_INT_MIN ||
2779 		    conf->vvc_adver_int > VRRP_MAX_ADVER_INT_MAX) {
2780 			vrrp_log(VRRP_DBG1, "vrrpd_modify(%s): invalid "
2781 			    "adver_interval %d", conf->vvc_name,
2782 			    conf->vvc_adver_int);
2783 			return (VRRP_EINVAL);
2784 		}
2785 	}
2786 
2787 	pri = vr->vvr_conf.vvc_pri;
2788 	if (mask & VRRP_CONF_PRIORITY) {
2789 		if (conf->vvc_pri < VRRP_PRI_MIN ||
2790 		    conf->vvc_pri > VRRP_PRI_OWNER) {
2791 			vrrp_log(VRRP_DBG1, "vrrpd_modify(%s): invalid "
2792 			    "priority %d", conf->vvc_name, conf->vvc_pri);
2793 			return (VRRP_EINVAL);
2794 		}
2795 		pri = conf->vvc_pri;
2796 	}
2797 
2798 	accept = vr->vvr_conf.vvc_accept;
2799 	if (mask & VRRP_CONF_ACCEPT)
2800 		accept = conf->vvc_accept;
2801 
2802 	if (pri == VRRP_PRI_OWNER && !accept) {
2803 		vrrp_log(VRRP_DBG1, "vrrpd_modify(%s): accept mode must be "
2804 		    "true for VRRP address owner", conf->vvc_name);
2805 		return (VRRP_EINVAL);
2806 	}
2807 
2808 	if ((mask & VRRP_CONF_ACCEPT) && (vr->vvr_conf.vvc_accept != accept)) {
2809 		err = vrrpd_set_noaccept(vr, !accept);
2810 		if (err != VRRP_SUCCESS) {
2811 			vrrp_log(VRRP_ERR, "vrrpd_modify(%s): access mode "
2812 			    "updating failed: %s", conf->vvc_name,
2813 			    vrrp_err2str(err));
2814 			return (err);
2815 		}
2816 		set_accept = _B_TRUE;
2817 	}
2818 
2819 	/*
2820 	 * Save the current configuration, so it can be restored if the
2821 	 * following fails.
2822 	 */
2823 	(void) memcpy(&savconf, &vr->vvr_conf, sizeof (vrrp_vr_conf_t));
2824 	if (mask & VRRP_CONF_PREEMPT)
2825 		vr->vvr_conf.vvc_preempt = conf->vvc_preempt;
2826 
2827 	if (mask & VRRP_CONF_ACCEPT)
2828 		vr->vvr_conf.vvc_accept = accept;
2829 
2830 	if (mask & VRRP_CONF_PRIORITY)
2831 		vr->vvr_conf.vvc_pri = pri;
2832 
2833 	if (mask & VRRP_CONF_INTERVAL)
2834 		vr->vvr_conf.vvc_adver_int = conf->vvc_adver_int;
2835 
2836 	err = vrrpd_updateconf(&vr->vvr_conf, VRRP_CONF_UPDATE);
2837 	if (err != VRRP_SUCCESS) {
2838 		vrrp_log(VRRP_ERR, "vrrpd_modify(%s): configuration update "
2839 		    "failed: %s", conf->vvc_name, vrrp_err2str(err));
2840 		if (set_accept)
2841 			(void) vrrpd_set_noaccept(vr, accept);
2842 		(void) memcpy(&vr->vvr_conf, &savconf, sizeof (vrrp_vr_conf_t));
2843 		return (err);
2844 	}
2845 
2846 	if ((mask & VRRP_CONF_PRIORITY) && (vr->vvr_state == VRRP_STATE_BACKUP))
2847 		vr->vvr_timeout = MASTER_DOWN_INTERVAL_VR(vr);
2848 
2849 	if ((mask & VRRP_CONF_INTERVAL) && (vr->vvr_state == VRRP_STATE_MASTER))
2850 		vr->vvr_timeout = conf->vvc_adver_int;
2851 
2852 	return (VRRP_SUCCESS);
2853 }
2854 
2855 static void
2856 vrrpd_list(vrid_t vrid, char *ifname, int af, vrrp_ret_list_t *ret,
2857     size_t *sizep)
2858 {
2859 	vrrp_vr_t	*vr;
2860 	char		*p = (char *)ret + sizeof (vrrp_ret_list_t);
2861 	size_t		size = (*sizep) - sizeof (vrrp_ret_list_t);
2862 
2863 	vrrp_log(VRRP_DBG0, "vrrpd_list(%d_%s_%s)", vrid, ifname, af_str(af));
2864 
2865 	ret->vrl_cnt = 0;
2866 	TAILQ_FOREACH(vr, &vrrp_vr_list, vvr_next) {
2867 		if (vrid !=  VRRP_VRID_NONE && vr->vvr_conf.vvc_vrid != vrid)
2868 			continue;
2869 
2870 		if (strlen(ifname) != 0 && strcmp(ifname,
2871 		    vr->vvr_conf.vvc_link) == 0) {
2872 			continue;
2873 		}
2874 
2875 		if ((af == AF_INET || af == AF_INET6) &&
2876 		    vr->vvr_conf.vvc_af != af)
2877 			continue;
2878 
2879 		if (size < VRRP_NAME_MAX) {
2880 			vrrp_log(VRRP_DBG1, "vrrpd_list(): buffer size too "
2881 			    "small to hold %d router names", ret->vrl_cnt);
2882 			*sizep = sizeof (vrrp_ret_list_t);
2883 			ret->vrl_err = VRRP_ETOOSMALL;
2884 			return;
2885 		}
2886 		(void) strlcpy(p, vr->vvr_conf.vvc_name, VRRP_NAME_MAX);
2887 		p += (strlen(vr->vvr_conf.vvc_name) + 1);
2888 		ret->vrl_cnt++;
2889 		size -= VRRP_NAME_MAX;
2890 	}
2891 
2892 	*sizep = sizeof (vrrp_ret_list_t) + ret->vrl_cnt * VRRP_NAME_MAX;
2893 	vrrp_log(VRRP_DBG1, "vrrpd_list() return %d", ret->vrl_cnt);
2894 	ret->vrl_err = VRRP_SUCCESS;
2895 }
2896 
2897 static void
2898 vrrpd_query(const char *vn, vrrp_ret_query_t *ret, size_t *sizep)
2899 {
2900 	vrrp_queryinfo_t	*infop;
2901 	vrrp_vr_t		*vr;
2902 	vrrp_intf_t		*vif;
2903 	vrrp_ip_t		*ip;
2904 	struct timeval		now;
2905 	uint32_t		vipcnt = 0;
2906 	size_t			size = *sizep;
2907 
2908 	vrrp_log(VRRP_DBG1, "vrrpd_query(%s)", vn);
2909 
2910 	if ((vr = vrrpd_lookup_vr_by_name(vn)) == NULL) {
2911 		vrrp_log(VRRP_DBG1, "vrrpd_query(): %s does not exist", vn);
2912 		*sizep = sizeof (vrrp_ret_query_t);
2913 		ret->vrq_err = VRRP_ENOTFOUND;
2914 		return;
2915 	}
2916 
2917 	/*
2918 	 * Get the virtual IP list if the router is not in the INIT state.
2919 	 */
2920 	if (vr->vvr_state != VRRP_STATE_INIT) {
2921 		vif = vr->vvr_vif;
2922 		TAILQ_FOREACH(ip, &vif->vvi_iplist, vip_next) {
2923 			vipcnt++;
2924 		}
2925 	}
2926 
2927 	*sizep = sizeof (vrrp_ret_query_t);
2928 	*sizep += (vipcnt == 0) ? 0 : (vipcnt - 1) * sizeof (vrrp_addr_t);
2929 	if (*sizep > size) {
2930 		vrrp_log(VRRP_ERR, "vrrpd_query(): not enough space to hold "
2931 		    "%d virtual IPs", vipcnt);
2932 		*sizep = sizeof (vrrp_ret_query_t);
2933 		ret->vrq_err = VRRP_ETOOSMALL;
2934 		return;
2935 	}
2936 
2937 	(void) gettimeofday(&now, NULL);
2938 
2939 	bzero(ret, *sizep);
2940 	infop = &ret->vrq_qinfo;
2941 	(void) memcpy(&infop->show_vi,
2942 	    &(vr->vvr_conf), sizeof (vrrp_vr_conf_t));
2943 	(void) memcpy(&infop->show_vs,
2944 	    &(vr->vvr_sinfo), sizeof (vrrp_stateinfo_t));
2945 	(void) strlcpy(infop->show_va.va_vnic, vr->vvr_vnic, MAXLINKNAMELEN);
2946 	infop->show_vt.vt_since_last_tran = timeval_to_milli(
2947 	    timeval_delta(now, vr->vvr_sinfo.vs_st_time));
2948 
2949 	if (vr->vvr_state == VRRP_STATE_INIT) {
2950 		ret->vrq_err = VRRP_SUCCESS;
2951 		return;
2952 	}
2953 
2954 	vipcnt = 0;
2955 	TAILQ_FOREACH(ip, &vif->vvi_iplist, vip_next) {
2956 		(void) memcpy(&infop->show_va.va_vips[vipcnt++],
2957 		    &ip->vip_addr, sizeof (vrrp_addr_t));
2958 	}
2959 	infop->show_va.va_vipcnt = vipcnt;
2960 
2961 	(void) memcpy(&infop->show_va.va_primary,
2962 	    &vr->vvr_pif->vvi_pip->vip_addr, sizeof (vrrp_addr_t));
2963 
2964 	(void) memcpy(&infop->show_vp, &(vr->vvr_peer), sizeof (vrrp_peer_t));
2965 
2966 	/*
2967 	 * Check whether there is a peer.
2968 	 */
2969 	if (!VRRPADDR_UNSPECIFIED(vr->vvr_conf.vvc_af,
2970 	    &(vr->vvr_peer.vp_addr))) {
2971 		infop->show_vt.vt_since_last_adv = timeval_to_milli(
2972 		    timeval_delta(now, vr->vvr_peer.vp_time));
2973 	}
2974 
2975 	if (vr->vvr_state == VRRP_STATE_BACKUP) {
2976 		infop->show_vt.vt_master_down_intv =
2977 		    MASTER_DOWN_INTERVAL_VR(vr);
2978 	}
2979 
2980 	ret->vrq_err = VRRP_SUCCESS;
2981 }
2982 
2983 /*
2984  * Build the VRRP packet (not including the IP header). Return the
2985  * payload length.
2986  *
2987  * If zero_pri is set to be B_TRUE, then this is the specical zero-priority
2988  * advertisement which is sent by the Master to indicate that it has been
2989  * stopped participating in VRRP.
2990  */
2991 static size_t
2992 vrrpd_build_vrrp(vrrp_vr_t *vr, uchar_t *buf, int buflen, boolean_t zero_pri)
2993 {
2994 	/* LINTED E_BAD_PTR_CAST_ALIGN */
2995 	vrrp_pkt_t	*vp = (vrrp_pkt_t *)buf;
2996 	/* LINTED E_BAD_PTR_CAST_ALIGN */
2997 	struct in_addr	*a4 = (struct in_addr *)(vp + 1);
2998 	/* LINTED E_BAD_PTR_CAST_ALIGN */
2999 	struct in6_addr *a6 = (struct in6_addr *)(vp + 1);
3000 	vrrp_intf_t	*vif = vr->vvr_vif;
3001 	vrrp_ip_t	*vip;
3002 	int		af = vif->vvi_af;
3003 	size_t		size = sizeof (vrrp_pkt_t);
3004 	uint16_t	rsvd_adver_int;
3005 	int		nip = 0;
3006 
3007 	vrrp_log(VRRP_DBG1, "vrrpd_build_vrrp(%s, %s_priority): intv %d",
3008 	    vr->vvr_conf.vvc_name, zero_pri ? "zero" : "non-zero",
3009 	    vr->vvr_conf.vvc_adver_int);
3010 
3011 	TAILQ_FOREACH(vip, &vif->vvi_iplist, vip_next) {
3012 		if ((size += ((af == AF_INET) ? sizeof (struct in_addr) :
3013 		    sizeof (struct in6_addr))) > buflen) {
3014 			vrrp_log(VRRP_ERR, "vrrpd_build_vrrp(%s): buffer size "
3015 			    "not big enough %d", vr->vvr_conf.vvc_name, size);
3016 			return (0);
3017 		}
3018 
3019 		if (af == AF_INET)
3020 			a4[nip++] = vip->vip_addr.in4.sin_addr;
3021 		else
3022 			a6[nip++] = vip->vip_addr.in6.sin6_addr;
3023 	}
3024 
3025 	if (nip == 0) {
3026 		vrrp_log(VRRP_ERR, "vrrpd_build_vrrp(%s): no virtual IP "
3027 		    "address", vr->vvr_conf.vvc_name);
3028 		return (0);
3029 	}
3030 
3031 	vp->vp_vers_type = (VRRP_VERSION << 4) | VRRP_PKT_ADVERT;
3032 	vp->vp_vrid = vr->vvr_conf.vvc_vrid;
3033 	vp->vp_prio = zero_pri ? VRRP_PRIO_ZERO : vr->vvr_conf.vvc_pri;
3034 
3035 	rsvd_adver_int = MSEC2CENTISEC(vr->vvr_conf.vvc_adver_int) & 0x0fff;
3036 	vp->vp_rsvd_adver_int = htons(rsvd_adver_int);
3037 	vp->vp_ipnum = nip;
3038 
3039 	/*
3040 	 * Set the checksum to 0 first, then caculate it.
3041 	 */
3042 	vp->vp_chksum = 0;
3043 	if (af == AF_INET) {
3044 		vp->vp_chksum = vrrp_cksum4(
3045 		    &vr->vvr_pif->vvi_pip->vip_addr.in4.sin_addr,
3046 		    &vrrp_muladdr4.in4.sin_addr, size, vp);
3047 	} else {
3048 		vp->vp_chksum = vrrp_cksum6(
3049 		    &vr->vvr_pif->vvi_pip->vip_addr.in6.sin6_addr,
3050 		    &vrrp_muladdr6.in6.sin6_addr, size, vp);
3051 	}
3052 
3053 	return (size);
3054 }
3055 
3056 /*
3057  * We need to build the IPv4 header on our own.
3058  */
3059 static vrrp_err_t
3060 vrrpd_send_adv_v4(vrrp_vr_t *vr, uchar_t *buf, size_t len, boolean_t zero_pri)
3061 {
3062 	/* LINTED E_BAD_PTR_CAST_ALIGN */
3063 	struct ip *ip = (struct ip *)buf;
3064 	size_t plen;
3065 
3066 	vrrp_log(VRRP_DBG1, "vrrpd_send_adv_v4(%s)", vr->vvr_conf.vvc_name);
3067 
3068 	if ((plen = vrrpd_build_vrrp(vr, buf + sizeof (struct ip),
3069 	    len - sizeof (struct ip), zero_pri)) == 0) {
3070 		return (VRRP_ETOOSMALL);
3071 	}
3072 
3073 	ip->ip_hl = sizeof (struct ip) >> 2;
3074 	ip->ip_v = IPV4_VERSION;
3075 	ip->ip_tos = 0;
3076 	plen += sizeof (struct ip);
3077 	ip->ip_len = htons(plen);
3078 	ip->ip_off = 0;
3079 	ip->ip_ttl = VRRP_IP_TTL;
3080 	ip->ip_p = IPPROTO_VRRP;
3081 	ip->ip_src = vr->vvr_pif->vvi_pip->vip_addr.in4.sin_addr;
3082 	ip->ip_dst = vrrp_muladdr4.in4.sin_addr;
3083 
3084 	/*
3085 	 * The kernel will set the IP cksum and the IPv4 identification.
3086 	 */
3087 	ip->ip_id = 0;
3088 	ip->ip_sum = 0;
3089 
3090 	if ((len = sendto(vr->vvr_vif->vvi_sockfd, buf, plen, 0,
3091 	    (const struct sockaddr *)&vrrp_muladdr4,
3092 	    sizeof (struct sockaddr_in))) != plen) {
3093 		vrrp_log(VRRP_ERR, "vrrpd_send_adv_v4(): sendto() on "
3094 		    "(vrid:%d, %s, %s) failed: %s sent:%d expect:%d",
3095 		    vr->vvr_conf.vvc_vrid, vr->vvr_vif->vvi_ifname,
3096 		    af_str(vr->vvr_conf.vvc_af), strerror(errno), len, plen);
3097 		return (VRRP_ESYS);
3098 	}
3099 
3100 	vrrp_log(VRRP_DBG1, "vrrpd_send_adv_v4(%s) succeed",
3101 	    vr->vvr_conf.vvc_name);
3102 	return (VRRP_SUCCESS);
3103 }
3104 
3105 static vrrp_err_t
3106 vrrpd_send_adv_v6(vrrp_vr_t *vr, uchar_t *buf, size_t len, boolean_t zero_pri)
3107 {
3108 	struct msghdr msg6;
3109 	size_t hoplimit_space = 0;
3110 	size_t pktinfo_space = 0;
3111 	size_t bufspace = 0;
3112 	struct in6_pktinfo *pktinfop;
3113 	struct cmsghdr *cmsgp;
3114 	uchar_t *cmsg_datap;
3115 	struct iovec iov;
3116 	size_t plen;
3117 
3118 	vrrp_log(VRRP_DBG1, "vrrpd_send_adv_v6(%s)", vr->vvr_conf.vvc_name);
3119 
3120 	if ((plen = vrrpd_build_vrrp(vr, buf, len, zero_pri)) == 0)
3121 		return (VRRP_ETOOSMALL);
3122 
3123 	msg6.msg_control = NULL;
3124 	msg6.msg_controllen = 0;
3125 
3126 	hoplimit_space = sizeof (int);
3127 	bufspace += sizeof (struct cmsghdr) + _MAX_ALIGNMENT +
3128 	    hoplimit_space + _MAX_ALIGNMENT;
3129 
3130 	pktinfo_space = sizeof (struct in6_pktinfo);
3131 	bufspace += sizeof (struct cmsghdr) + _MAX_ALIGNMENT +
3132 	    pktinfo_space + _MAX_ALIGNMENT;
3133 
3134 	/*
3135 	 * We need to temporarily set the msg6.msg_controllen to bufspace
3136 	 * (we will later trim it to actual length used). This is needed because
3137 	 * CMSG_NXTHDR() uses it to check we have not exceeded the bounds.
3138 	 */
3139 	bufspace += sizeof (struct cmsghdr);
3140 	msg6.msg_controllen = bufspace;
3141 
3142 	msg6.msg_control = (struct cmsghdr *)malloc(bufspace);
3143 	if (msg6.msg_control == NULL) {
3144 		vrrp_log(VRRP_ERR, "vrrpd_send_adv_v6(%s): memory allocation "
3145 		    "failed: %s", vr->vvr_conf.vvc_name, strerror(errno));
3146 		return (VRRP_ENOMEM);
3147 	}
3148 
3149 	cmsgp = CMSG_FIRSTHDR(&msg6);
3150 
3151 	cmsgp->cmsg_level = IPPROTO_IPV6;
3152 	cmsgp->cmsg_type = IPV6_HOPLIMIT;
3153 	cmsg_datap = CMSG_DATA(cmsgp);
3154 	/* LINTED */
3155 	*(int *)cmsg_datap = VRRP_IP_TTL;
3156 	cmsgp->cmsg_len = cmsg_datap + hoplimit_space - (uchar_t *)cmsgp;
3157 	cmsgp = CMSG_NXTHDR(&msg6, cmsgp);
3158 
3159 	cmsgp->cmsg_level = IPPROTO_IPV6;
3160 	cmsgp->cmsg_type = IPV6_PKTINFO;
3161 	cmsg_datap = CMSG_DATA(cmsgp);
3162 
3163 	/* LINTED */
3164 	pktinfop = (struct in6_pktinfo *)cmsg_datap;
3165 	/*
3166 	 * We don't know if pktinfop->ipi6_addr is aligned properly,
3167 	 * therefore let's use bcopy, instead of assignment.
3168 	 */
3169 	(void) bcopy(&vr->vvr_pif->vvi_pip->vip_addr.in6.sin6_addr,
3170 	    &pktinfop->ipi6_addr, sizeof (struct in6_addr));
3171 
3172 	/*
3173 	 *  We can assume pktinfop->ipi6_ifindex is 32 bit aligned.
3174 	 */
3175 	pktinfop->ipi6_ifindex = vr->vvr_vif->vvi_ifindex;
3176 	cmsgp->cmsg_len = cmsg_datap + pktinfo_space - (uchar_t *)cmsgp;
3177 	cmsgp = CMSG_NXTHDR(&msg6, cmsgp);
3178 	msg6.msg_controllen = (char *)cmsgp - (char *)msg6.msg_control;
3179 
3180 	msg6.msg_name = &vrrp_muladdr6;
3181 	msg6.msg_namelen = sizeof (struct sockaddr_in6);
3182 
3183 	iov.iov_base = buf;
3184 	iov.iov_len = plen;
3185 	msg6.msg_iov = &iov;
3186 	msg6.msg_iovlen = 1;
3187 
3188 	if ((len = sendmsg(vr->vvr_vif->vvi_sockfd,
3189 	    (const struct msghdr *)&msg6, 0)) != plen) {
3190 		vrrp_log(VRRP_ERR, "vrrpd_send_adv_v6(%s): sendmsg() failed: "
3191 		    "%s expect %d sent %d", vr->vvr_conf.vvc_name,
3192 		    strerror(errno), plen, len);
3193 		(void) free(msg6.msg_control);
3194 		return (VRRP_ESYS);
3195 	}
3196 
3197 	vrrp_log(VRRP_DBG1, "vrrpd_send_adv_v6(%s) succeed",
3198 	    vr->vvr_conf.vvc_name);
3199 	(void) free(msg6.msg_control);
3200 	return (VRRP_SUCCESS);
3201 }
3202 
3203 /*
3204  * Send the VRRP advertisement packets.
3205  */
3206 static vrrp_err_t
3207 vrrpd_send_adv(vrrp_vr_t *vr, boolean_t zero_pri)
3208 {
3209 	uint64_t buf[(IP_MAXPACKET + 1)/8];
3210 
3211 	vrrp_log(VRRP_DBG1, "vrrpd_send_adv(%s, %s_priority)",
3212 	    vr->vvr_conf.vvc_name, zero_pri ? "zero" : "non_zero");
3213 
3214 	assert(vr->vvr_pif->vvi_pip != NULL);
3215 
3216 	if (vr->vvr_pif->vvi_pip == NULL) {
3217 		vrrp_log(VRRP_DBG0, "vrrpd_send_adv(%s): no primary IP "
3218 		    "address", vr->vvr_conf.vvc_name);
3219 		return (VRRP_EINVAL);
3220 	}
3221 
3222 	if (vr->vvr_conf.vvc_af == AF_INET) {
3223 		return (vrrpd_send_adv_v4(vr, (uchar_t *)buf,
3224 		    sizeof (buf), zero_pri));
3225 	} else {
3226 		return (vrrpd_send_adv_v6(vr, (uchar_t *)buf,
3227 		    sizeof (buf), zero_pri));
3228 	}
3229 }
3230 
3231 static void
3232 vrrpd_process_adv(vrrp_vr_t *vr, vrrp_addr_t *from, vrrp_pkt_t *vp)
3233 {
3234 	vrrp_vr_conf_t *conf = &vr->vvr_conf;
3235 	char		peer[INET6_ADDRSTRLEN];
3236 	char		local[INET6_ADDRSTRLEN];
3237 	int		addr_cmp;
3238 	uint16_t	peer_adver_int;
3239 
3240 	/* LINTED E_CONSTANT_CONDITION */
3241 	VRRPADDR2STR(vr->vvr_conf.vvc_af, from, peer, INET6_ADDRSTRLEN,
3242 	    _B_FALSE);
3243 	vrrp_log(VRRP_DBG1, "vrrpd_process_adv(%s) from %s", conf->vvc_name,
3244 	    peer);
3245 
3246 	if (vr->vvr_state <= VRRP_STATE_INIT) {
3247 		vrrp_log(VRRP_DBG1, "vrrpd_process_adv(%s): state: %s, not "
3248 		    "ready", conf->vvc_name, vrrp_state2str(vr->vvr_state));
3249 		return;
3250 	}
3251 
3252 	peer_adver_int = CENTISEC2MSEC(ntohs(vp->vp_rsvd_adver_int) & 0x0fff);
3253 
3254 	/* LINTED E_CONSTANT_CONDITION */
3255 	VRRPADDR2STR(vr->vvr_pif->vvi_af, &vr->vvr_pif->vvi_pip->vip_addr,
3256 	    local, INET6_ADDRSTRLEN, _B_FALSE);
3257 	vrrp_log(VRRP_DBG1, "vrrpd_process_adv(%s): local/state/pri"
3258 	    "(%s/%s/%d) peer/pri/intv(%s/%d/%d)", conf->vvc_name, local,
3259 	    vrrp_state2str(vr->vvr_state), conf->vvc_pri, peer,
3260 	    vp->vp_prio, peer_adver_int);
3261 
3262 	addr_cmp = ipaddr_cmp(vr->vvr_pif->vvi_af, from,
3263 	    &vr->vvr_pif->vvi_pip->vip_addr);
3264 	if (addr_cmp == 0) {
3265 		vrrp_log(VRRP_DBG1, "vrrpd_process_adv(%s): local message",
3266 		    conf->vvc_name);
3267 		return;
3268 	} else if (conf->vvc_pri == vp->vp_prio) {
3269 		vrrp_log(VRRP_DBG1, "vrrpd_process_adv(%s): peer IP %s is %s"
3270 		    " than the local IP %s", conf->vvc_name, peer,
3271 		    addr_cmp > 0 ? "greater" : "less", local);
3272 	}
3273 
3274 	if (conf->vvc_pri == 255) {
3275 		vrrp_log(VRRP_ERR, "vrrpd_process_adv(%s): virtual address "
3276 		    "owner received advertisement from %s", conf->vvc_name,
3277 		    peer);
3278 		return;
3279 	}
3280 
3281 	(void) gettimeofday(&vr->vvr_peer_time, NULL);
3282 	(void) memcpy(&vr->vvr_peer_addr, from, sizeof (vrrp_addr_t));
3283 	vr->vvr_peer_prio =