17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5d04ccbbcarlsonj * Common Development and Distribution License (the "License").
6d04ccbbcarlsonj * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
22d04ccbbcarlsonj * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bdstevel@tonic-gate * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate#include "defs.h"
297c478bdstevel@tonic-gate#include "tables.h"
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate/*
327c478bdstevel@tonic-gate * Parse the config file which consists of entries of the form:
337c478bdstevel@tonic-gate *	ifdefault	[<variable> <value>]*
347c478bdstevel@tonic-gate *	prefixdefault	[<variable> <value>]*
357c478bdstevel@tonic-gate *	if <ifname>	[<variable> <value>]*
367c478bdstevel@tonic-gate *	prefix <prefix>/<length> <ifname>	[<variable> <value>]*
377c478bdstevel@tonic-gate *
387c478bdstevel@tonic-gate * All "ifdefault" and "prefixdefault" entries must preceed any
397c478bdstevel@tonic-gate * "if" and "prefix" entries.
407c478bdstevel@tonic-gate *
417c478bdstevel@tonic-gate * Values (such as expiry dates) which contain white space
427c478bdstevel@tonic-gate * can be quoted with single or double quotes.
437c478bdstevel@tonic-gate */
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate/* maximum length of messages we send to syslog */
467c478bdstevel@tonic-gate#define	NDPD_LOGMSGSIZE	1024
477c478bdstevel@tonic-gatetypedef	boolean_t	(*pfb_t)(char *, uint_t *);
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gatestruct configinfo {
507c478bdstevel@tonic-gate	char	*ci_name;
517c478bdstevel@tonic-gate	uint_t	ci_min;		/* 0: no min check */
527c478bdstevel@tonic-gate	uint_t	ci_max;		/* ~0U: no max check */
537c478bdstevel@tonic-gate	uint_t	ci_default;
547c478bdstevel@tonic-gate	uint_t	ci_index;	/* Into result array */
557c478bdstevel@tonic-gate	pfb_t	ci_parsefunc;	/* Parse function returns -1 on failure */
567c478bdstevel@tonic-gate};
577c478bdstevel@tonic-gate
587c478bdstevel@tonic-gateenum config_type { CONFIG_IF, CONFIG_PREFIX};
597c478bdstevel@tonic-gatetypedef enum config_type config_type_t;
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gatestatic void set_protocol_defaults(void);
627c478bdstevel@tonic-gatestatic void print_defaults(void);
637c478bdstevel@tonic-gatestatic void parse_var_value(config_type_t, struct configinfo *, char *, char *,
647c478bdstevel@tonic-gate    struct confvar *);
657c478bdstevel@tonic-gatestatic void parse_default(config_type_t, struct configinfo *, char **, int,
667c478bdstevel@tonic-gate    struct confvar *);
677c478bdstevel@tonic-gatestatic void parse_if(struct configinfo *, char **, int);
687c478bdstevel@tonic-gatestatic void parse_prefix(struct configinfo *, char **, int);
697c478bdstevel@tonic-gatestatic boolean_t parse_onoff(char *, uint_t *);	/* boolean */
707c478bdstevel@tonic-gatestatic boolean_t parse_int(char *, uint_t *);	/* integer */
717c478bdstevel@tonic-gatestatic boolean_t parse_ms(char *, uint_t *);	/* milliseconds */
727c478bdstevel@tonic-gatestatic boolean_t parse_s(char *, uint_t *);	/* seconds */
737c478bdstevel@tonic-gatestatic boolean_t parse_date(char *, uint_t *);	/* date format */
747c478bdstevel@tonic-gatestatic void conferr(char *fmt, ...);
757c478bdstevel@tonic-gatestatic FILE *open_conffile(char *filename);
767c478bdstevel@tonic-gatestatic int parse_line(char *line, char *argvec[], int argcount);
777c478bdstevel@tonic-gatestatic int readline(FILE *fp, char *line, int length);
787c478bdstevel@tonic-gatestatic int parse_addrprefix(char *strin, struct in6_addr *in6);
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gate/*
817c478bdstevel@tonic-gate * Per interface configuration variables.
827c478bdstevel@tonic-gate * Min, max, and default values are from RFC 2461.
837c478bdstevel@tonic-gate */
847c478bdstevel@tonic-gatestatic struct configinfo iflist[] = {
857c478bdstevel@tonic-gate	/* Name, Min, Max, Default, Index */
867c478bdstevel@tonic-gate	{ "DupAddrDetectTransmits", 0, 100, 1, I_DupAddrDetectTransmits,
877c478bdstevel@tonic-gate	parse_int },
887c478bdstevel@tonic-gate	{ "AdvSendAdvertisements", 0, 1, 0, I_AdvSendAdvertisements,
897c478bdstevel@tonic-gate	parse_onoff },
907c478bdstevel@tonic-gate	{ "MaxRtrAdvInterval", 4, 1800, 600, I_MaxRtrAdvInterval, parse_s },
917c478bdstevel@tonic-gate	{ "MinRtrAdvInterval", 3, 1350, 200, I_MinRtrAdvInterval, parse_s },
927c478bdstevel@tonic-gate	/*
937c478bdstevel@tonic-gate	 * No greater than .75 * MaxRtrAdvInterval.
947c478bdstevel@tonic-gate	 * Default: 0.33 * MaxRtrAdvInterval
957c478bdstevel@tonic-gate	 */
967c478bdstevel@tonic-gate	{ "AdvManagedFlag", 0, 1, 0, I_AdvManagedFlag, parse_onoff },
977c478bdstevel@tonic-gate	{ "AdvOtherConfigFlag", 0, 1, 0, I_AdvOtherConfigFlag, parse_onoff },
987c478bdstevel@tonic-gate	{ "AdvLinkMTU", IPV6_MIN_MTU, 65535, 0, I_AdvLinkMTU, parse_int },
997c478bdstevel@tonic-gate	{ "AdvReachableTime", 0, 3600000, 0, I_AdvReachableTime, parse_ms },
1007c478bdstevel@tonic-gate	{ "AdvRetransTimer", 0, ~0U, 0, I_AdvRetransTimer, parse_ms },
1017c478bdstevel@tonic-gate	{ "AdvCurHopLimit", 0, 255, 0, I_AdvCurHopLimit, parse_int },
1027c478bdstevel@tonic-gate	{ "AdvDefaultLifetime", 0, 9000, 1800, I_AdvDefaultLifetime, parse_s },
1037c478bdstevel@tonic-gate	/*
1047c478bdstevel@tonic-gate	 * MUST be either zero or between MaxRtrAdvInterval and 9000 seconds.
1057c478bdstevel@tonic-gate	 * Default: 3 * MaxRtrAdvInterval
1067c478bdstevel@tonic-gate	 */
1077c478bdstevel@tonic-gate	{ "StatelessAddrConf", 0, 1, 1, I_StatelessAddrConf, parse_onoff },
108d04ccbbcarlsonj	{ "StatefulAddrConf", 0, 1, 1, I_StatefulAddrConf, parse_onoff },
1097c478bdstevel@tonic-gate	/*
1107c478bdstevel@tonic-gate	 * Tmp* variables from RFC 3041, where defaults are defined.
1117c478bdstevel@tonic-gate	 */
1127c478bdstevel@tonic-gate	{ "TmpAddrsEnabled", 0, 1, 0, I_TmpAddrsEnabled, parse_onoff },
1137c478bdstevel@tonic-gate	{ "TmpValidLifetime", 0, ~0U, 604800, I_TmpValidLifetime, parse_s },
1147c478bdstevel@tonic-gate	{ "TmpPreferredLifetime", 0, ~0U, 86400, I_TmpPreferredLifetime,
1157c478bdstevel@tonic-gate	parse_s },
1167c478bdstevel@tonic-gate	{ "TmpRegenAdvance", 0, 60, 5, I_TmpRegenAdvance, parse_s },
1177c478bdstevel@tonic-gate	{ "TmpMaxDesyncFactor", 0, 600, 600, I_TmpMaxDesyncFactor, parse_s },
1187c478bdstevel@tonic-gate	{ NULL, 0, 0, 0, 0 }
1197c478bdstevel@tonic-gate};
1207c478bdstevel@tonic-gate
1217c478bdstevel@tonic-gate/*
1227c478bdstevel@tonic-gate * Per prefix: AdvPrefixList configuration variables.
1237c478bdstevel@tonic-gate * Min, max, and default values are from RFC 2461.
1247c478bdstevel@tonic-gate */
1257c478bdstevel@tonic-gatestatic struct configinfo prefixlist[] = {
1267c478bdstevel@tonic-gate	/* Name, Min, Max, Default, Index */
1277c478bdstevel@tonic-gate	{ "AdvValidLifetime", 0, ~0U, 2592000, I_AdvValidLifetime,
1287c478bdstevel@tonic-gate	parse_s },
1297c478bdstevel@tonic-gate	{ "AdvOnLinkFlag", 0, 1, 1, I_AdvOnLinkFlag, parse_onoff },
1307c478bdstevel@tonic-gate	{ "AdvPreferredLifetime", 0, ~0U, 604800, I_AdvPreferredLifetime,
1317c478bdstevel@tonic-gate	parse_s},
1327c478bdstevel@tonic-gate	{ "AdvAutonomousFlag", 0, 1, 1, I_AdvAutonomousFlag, parse_onoff },
1337c478bdstevel@tonic-gate	{ "AdvValidExpiration", 0, ~0U, 0, I_AdvValidExpiration,
1347c478bdstevel@tonic-gate	parse_date },
1357c478bdstevel@tonic-gate	{ "AdvPreferredExpiration", 0, ~0U, 0, I_AdvPreferredExpiration,
1367c478bdstevel@tonic-gate	parse_date},
1377c478bdstevel@tonic-gate	{ NULL, 0, 0, 0, 0 },
1387c478bdstevel@tonic-gate};
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gate/*
1417c478bdstevel@tonic-gate * Data structures used to merge above protocol defaults
1427c478bdstevel@tonic-gate * with defaults specified in the configuration file.
1437c478bdstevel@tonic-gate * ifdefault is not static because new interfaces can be
1447c478bdstevel@tonic-gate * created outside of the configuration context.
1457c478bdstevel@tonic-gate */
1467c478bdstevel@tonic-gatestruct confvar ifdefaults[I_IFSIZE];
1477c478bdstevel@tonic-gatestatic struct confvar prefixdefaults[I_PREFIXSIZE];
1487c478bdstevel@tonic-gate
1497c478bdstevel@tonic-gatestatic char	conf_filename[MAXPATHLEN];
1507c478bdstevel@tonic-gatestatic int	lineno;
1517c478bdstevel@tonic-gate
1527c478bdstevel@tonic-gate/*
1537c478bdstevel@tonic-gate * Checks for violations of section 5.5.3 (c) of RFC 2462.
1547c478bdstevel@tonic-gate */
1557c478bdstevel@tonic-gatestatic void
1567c478bdstevel@tonic-gatecheck_var_consistency(struct confvar *cv, void *save, int size)
1577c478bdstevel@tonic-gate{
1587c478bdstevel@tonic-gate	boolean_t rollback = _B_FALSE;
1597c478bdstevel@tonic-gate	int prefl, prefe, valid;
1607c478bdstevel@tonic-gate
1617c478bdstevel@tonic-gate	prefl = cv[I_AdvPreferredLifetime].cf_value;
1627c478bdstevel@tonic-gate	prefe = cv[I_AdvPreferredExpiration].cf_value;
1637c478bdstevel@tonic-gate	valid = cv[I_AdvValidLifetime].cf_value;
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate	if (prefl > valid) {
1667c478bdstevel@tonic-gate		conferr("AdvPreferredLifetime (%u) is greater than "
1677c478bdstevel@tonic-gate		    "valid lifetime (%u)\n", prefl, valid);
1687c478bdstevel@tonic-gate		rollback = _B_TRUE;
1697c478bdstevel@tonic-gate	}
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate	if (prefe > valid) {
1727c478bdstevel@tonic-gate		conferr("AdvPreferredExpiration (%u) is greater than "
1737c478bdstevel@tonic-gate		    "valid lifetime (%u)\n", prefe, valid);
1747c478bdstevel@tonic-gate		rollback = _B_TRUE;
1757c478bdstevel@tonic-gate	}
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate	if (rollback) {
1787c478bdstevel@tonic-gate		(void) memcpy(cv, save, size);
1797c478bdstevel@tonic-gate	}
1807c478bdstevel@tonic-gate}
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate/*
1837c478bdstevel@tonic-gate * Check for invalid lifetime values for RFC3041 addresses
1847c478bdstevel@tonic-gate */
1857c478bdstevel@tonic-gatestatic void
1867c478bdstevel@tonic-gatecheck_if_var_consistency(struct confvar *cv, void *save, int size)
1877c478bdstevel@tonic-gate{
1887c478bdstevel@tonic-gate	boolean_t rollback = _B_FALSE;
1897c478bdstevel@tonic-gate	int tpref, tvalid, tdesync, tregen;
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate	tpref = cv[I_TmpPreferredLifetime].cf_value;
1927c478bdstevel@tonic-gate	tvalid = cv[I_TmpValidLifetime].cf_value;
1937c478bdstevel@tonic-gate	tdesync = cv[I_TmpMaxDesyncFactor].cf_value;
1947c478bdstevel@tonic-gate	tregen = cv[I_TmpRegenAdvance].cf_value;
1957c478bdstevel@tonic-gate
1967c478bdstevel@tonic-gate	/*
1977c478bdstevel@tonic-gate	 * Only need to do this if tmp addrs are enabled.
1987c478bdstevel@tonic-gate	 */
1997c478bdstevel@tonic-gate	if (cv[I_TmpAddrsEnabled].cf_value == 0)
2007c478bdstevel@tonic-gate		return;
2017c478bdstevel@tonic-gate
2027c478bdstevel@tonic-gate	if (tdesync > tpref) {
2037c478bdstevel@tonic-gate		conferr("TmpDesyncFactor (%u) is greater than "
2047c478bdstevel@tonic-gate		    "TmpPreferredLifetime (%u)\n", tdesync, tpref);
2057c478bdstevel@tonic-gate		rollback = _B_TRUE;
2067c478bdstevel@tonic-gate	}
2077c478bdstevel@tonic-gate
2087c478bdstevel@tonic-gate	if (tpref > tvalid) {
2097c478bdstevel@tonic-gate		conferr("TmpPreferredLifetime (%u) is greater than "
2107c478bdstevel@tonic-gate		    "TmpValidLifetime (%u)\n", tpref, tvalid);
2117c478bdstevel@tonic-gate		rollback = _B_TRUE;
2127c478bdstevel@tonic-gate	}
2137c478bdstevel@tonic-gate
2147c478bdstevel@tonic-gate	if (tregen > tvalid) {
2157c478bdstevel@tonic-gate		conferr("TmpRegenAdvance (%u) is greater than "
2167c478bdstevel@tonic-gate		    "TmpValidLifetime (%u)\n", tregen, tvalid);
2177c478bdstevel@tonic-gate		rollback = _B_TRUE;
2187c478bdstevel@tonic-gate	}
2197c478bdstevel@tonic-gate
2207c478bdstevel@tonic-gate	if (rollback) {
2217c478bdstevel@tonic-gate		(void) memcpy(cv, save, size);
2227c478bdstevel@tonic-gate	}
2237c478bdstevel@tonic-gate}
2247c478bdstevel@tonic-gate
2257c478bdstevel@tonic-gateint
2267c478bdstevel@tonic-gateparse_config(char *config_file, boolean_t file_required)
2277c478bdstevel@tonic-gate{
2287c478bdstevel@tonic-gate	FILE *fp;
2297c478bdstevel@tonic-gate	char line[MAXLINELEN];
2307c478bdstevel@tonic-gate	char pline[MAXLINELEN];
2317c478bdstevel@tonic-gate	int argcount;
2327c478bdstevel@tonic-gate	char *argvec[MAXARGSPERLINE];
2337c478bdstevel@tonic-gate	int defaultdone = 0;	/* Set when first non-default command found */
2347c478bdstevel@tonic-gate
2357c478bdstevel@tonic-gate	if (debug & D_CONFIG)
2367c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "parse_config()\n");
2377c478bdstevel@tonic-gate
2387c478bdstevel@tonic-gate	set_protocol_defaults();
2397c478bdstevel@tonic-gate	if (debug & D_DEFAULTS)
2407c478bdstevel@tonic-gate		print_defaults();
2417c478bdstevel@tonic-gate
2427c478bdstevel@tonic-gate	fp = open_conffile(config_file);
2437c478bdstevel@tonic-gate	if (fp == NULL) {
2447c478bdstevel@tonic-gate		if (errno == ENOENT && !file_required)
2457c478bdstevel@tonic-gate			return (0);
2467c478bdstevel@tonic-gate		logperror(config_file);
2477c478bdstevel@tonic-gate		return (-1);
2487c478bdstevel@tonic-gate	}
2497c478bdstevel@tonic-gate	while (readline(fp, line, sizeof (line)) != 0) {
2507c478bdstevel@tonic-gate		(void) strncpy(pline, line, sizeof (pline));
2517c478bdstevel@tonic-gate		pline[sizeof (pline) - 1] = '\0';	/* NULL terminate */
2527c478bdstevel@tonic-gate		argcount = parse_line(pline, argvec,
2537c478bdstevel@tonic-gate		    sizeof (argvec) / sizeof (argvec[0]));
2547c478bdstevel@tonic-gate		if (debug & D_PARSE) {
2557c478bdstevel@tonic-gate			int i;
2567c478bdstevel@tonic-gate
2577c478bdstevel@tonic-gate			logmsg(LOG_DEBUG, "scanned %d args\n", argcount);
2587c478bdstevel@tonic-gate			for (i = 0; i < argcount; i++)
2597c478bdstevel@tonic-gate				logmsg(LOG_DEBUG, "arg[%d]: %s\n",
2607c478bdstevel@tonic-gate				    i, argvec[i]);
2617c478bdstevel@tonic-gate		}
2627c478bdstevel@tonic-gate		if (argcount == 0) {
2637c478bdstevel@tonic-gate			/* Empty line - or comment only line */
2647c478bdstevel@tonic-gate			continue;
2657c478bdstevel@tonic-gate		}
2667c478bdstevel@tonic-gate		if (strcmp(argvec[0], "ifdefault") == 0) {
2677c478bdstevel@tonic-gate			char save[sizeof (ifdefaults)];
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate			if (defaultdone) {
2707c478bdstevel@tonic-gate				conferr("ifdefault after non-default "
2717c478bdstevel@tonic-gate				    "command\n");
2727c478bdstevel@tonic-gate				continue;
2737c478bdstevel@tonic-gate			}
2747c478bdstevel@tonic-gate			/*
2757c478bdstevel@tonic-gate			 * Save existing values in case what we read is
2767c478bdstevel@tonic-gate			 * invalid and we need to restore previous settings.
2777c478bdstevel@tonic-gate			 */
2787c478bdstevel@tonic-gate			(void) memcpy(save, ifdefaults, sizeof (ifdefaults));
2797c478bdstevel@tonic-gate			parse_default(CONFIG_IF, iflist, argvec+1, argcount-1,
2807c478bdstevel@tonic-gate			    ifdefaults);
2817c478bdstevel@tonic-gate			check_if_var_consistency(ifdefaults, save,
2827c478bdstevel@tonic-gate			    sizeof (save));
2837c478bdstevel@tonic-gate		} else if (strcmp(argvec[0], "prefixdefault") == 0) {
2847c478bdstevel@tonic-gate			char save[sizeof (prefixdefaults)];
2857c478bdstevel@tonic-gate
2867c478bdstevel@tonic-gate			if (defaultdone) {
2877c478bdstevel@tonic-gate				conferr("prefixdefault after non-default "
2887c478bdstevel@tonic-gate				    "command\n");
2897c478bdstevel@tonic-gate				continue;
2907c478bdstevel@tonic-gate			}
2917c478bdstevel@tonic-gate			/*
2927c478bdstevel@tonic-gate			 * Save existing values in case what we read is
2937c478bdstevel@tonic-gate			 * invalid and we need to restore previous settings.
2947c478bdstevel@tonic-gate			 */
2957c478bdstevel@tonic-gate			(void) memcpy(save, prefixdefaults,
2967c478bdstevel@tonic-gate			    sizeof (prefixdefaults));
2977c478bdstevel@tonic-gate			parse_default(CONFIG_PREFIX, prefixlist, argvec+1,
2987c478bdstevel@tonic-gate			    argcount-1, prefixdefaults);
2997c478bdstevel@tonic-gate			check_var_consistency(prefixdefaults, save,
3007c478bdstevel@tonic-gate			    sizeof (save));
3017c478bdstevel@tonic-gate		} else if (strcmp(argvec[0], "if") == 0) {
3027c478bdstevel@tonic-gate			defaultdone = 1;
3037c478bdstevel@tonic-gate			parse_if(iflist, argvec+1, argcount-1);
3047c478bdstevel@tonic-gate		} else if (strcmp(argvec[0], "prefix") == 0) {
3057c478bdstevel@tonic-gate			defaultdone = 1;
3067c478bdstevel@tonic-gate			parse_prefix(prefixlist, argvec+1, argcount-1);
3077c478bdstevel@tonic-gate		} else {
3087c478bdstevel@tonic-gate			conferr("Unknown command: %s\n", argvec[0]);
3097c478bdstevel@tonic-gate		}
3107c478bdstevel@tonic-gate	}
3117c478bdstevel@tonic-gate	(void) fclose(fp);
3127c478bdstevel@tonic-gate	if (debug & D_DEFAULTS)
3137c478bdstevel@tonic-gate		print_defaults();
3147c478bdstevel@tonic-gate	return (0);
3157c478bdstevel@tonic-gate}
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gate/*
3187c478bdstevel@tonic-gate * Extract the defaults from the configinfo tables to initialize
3197c478bdstevel@tonic-gate * the ifdefaults and prefixdefaults arrays.
3207c478bdstevel@tonic-gate * The arrays are needed to track which defaults have been changed
3217c478bdstevel@tonic-gate * by the config file.
3227c478bdstevel@tonic-gate */
3237c478bdstevel@tonic-gatestatic void
3247c478bdstevel@tonic-gateset_protocol_defaults(void)
3257c478bdstevel@tonic-gate{
3267c478bdstevel@tonic-gate	struct configinfo *cip;
3277c478bdstevel@tonic-gate
3287c478bdstevel@tonic-gate	if (debug & D_DEFAULTS)
3297c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "extract_protocol_defaults\n");
3307c478bdstevel@tonic-gate	for (cip = iflist; cip->ci_name != NULL; cip++) {
3317c478bdstevel@tonic-gate		ifdefaults[cip->ci_index].cf_value = cip->ci_default;
3327c478bdstevel@tonic-gate		ifdefaults[cip->ci_index].cf_notdefault = _B_FALSE;
3337c478bdstevel@tonic-gate	}
3347c478bdstevel@tonic-gate	for (cip = prefixlist; cip->ci_name != NULL; cip++) {
3357c478bdstevel@tonic-gate		prefixdefaults[cip->ci_index].cf_value = cip->ci_default;
3367c478bdstevel@tonic-gate		prefixdefaults[cip->ci_index].cf_notdefault = _B_FALSE;
3377c478bdstevel@tonic-gate	}
3387c478bdstevel@tonic-gate}
3397c478bdstevel@tonic-gate
3407c478bdstevel@tonic-gatevoid
3417c478bdstevel@tonic-gateprint_iflist(struct confvar *confvar)
3427c478bdstevel@tonic-gate{
3437c478bdstevel@tonic-gate	struct configinfo *cip;
3447c478bdstevel@tonic-gate
3457c478bdstevel@tonic-gate	for (cip = iflist; cip->ci_name != NULL; cip++) {
3467c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "\t%s min %u max %u def %u value %u set %d\n",
3477c478bdstevel@tonic-gate		    cip->ci_name, cip->ci_min, cip->ci_max, cip->ci_default,
3487c478bdstevel@tonic-gate		    confvar[cip->ci_index].cf_value,
3497c478bdstevel@tonic-gate		    confvar[cip->ci_index].cf_notdefault);
3507c478bdstevel@tonic-gate	}
3517c478bdstevel@tonic-gate}
3527c478bdstevel@tonic-gate
3537c478bdstevel@tonic-gatevoid
3547c478bdstevel@tonic-gateprint_prefixlist(struct confvar *confvar)
3557c478bdstevel@tonic-gate{
3567c478bdstevel@tonic-gate	struct configinfo *cip;
3577c478bdstevel@tonic-gate
3587c478bdstevel@tonic-gate	for (cip = prefixlist; cip->ci_name != NULL; cip++) {
3597c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "\t%s min %u max %u def %u value %u set %d\n",
3607c478bdstevel@tonic-gate		    cip->ci_name, cip->ci_min, cip->ci_max, cip->ci_default,
3617c478bdstevel@tonic-gate		    confvar[cip->ci_index].cf_value,
3627c478bdstevel@tonic-gate		    confvar[cip->ci_index].cf_notdefault);
3637c478bdstevel@tonic-gate	}
3647c478bdstevel@tonic-gate}
3657c478bdstevel@tonic-gate
3667c478bdstevel@tonic-gate
3677c478bdstevel@tonic-gatestatic void
3687c478bdstevel@tonic-gateprint_defaults(void)
3697c478bdstevel@tonic-gate{
3707c478bdstevel@tonic-gate	logmsg(LOG_DEBUG, "Default interface variables:\n");
3717c478bdstevel@tonic-gate	print_iflist(ifdefaults);
3727c478bdstevel@tonic-gate	logmsg(LOG_DEBUG, "Default prefix variables:\n");
3737c478bdstevel@tonic-gate	print_prefixlist(prefixdefaults);
3747c478bdstevel@tonic-gate}
3757c478bdstevel@tonic-gate
3767c478bdstevel@tonic-gate/*
3777c478bdstevel@tonic-gate * Read from fp. Handle \ at the end of the line by joining lines together.
3787c478bdstevel@tonic-gate * Return 0 on EOF.
3797c478bdstevel@tonic-gate */
3807c478bdstevel@tonic-gatestatic int
3817c478bdstevel@tonic-gatereadline(FILE *fp, char *line, int length)
3827c478bdstevel@tonic-gate{
3837c478bdstevel@tonic-gate	int got = 0;
3847c478bdstevel@tonic-gate
3857c478bdstevel@tonic-gateretry:
3867c478bdstevel@tonic-gate	errno = 0;
3877c478bdstevel@tonic-gate	if (fgets(line, length, fp) == NULL) {
3887c478bdstevel@tonic-gate		if (errno == EINTR)
3897c478bdstevel@tonic-gate			goto retry;
3907c478bdstevel@tonic-gate		if (got != 0)
3917c478bdstevel@tonic-gate			return (1);
3927c478bdstevel@tonic-gate		else
3937c478bdstevel@tonic-gate			return (0);
3947c478bdstevel@tonic-gate	}
3957c478bdstevel@tonic-gate	lineno++;
3967c478bdstevel@tonic-gate	got = strlen(line);
3977c478bdstevel@tonic-gate	/* Look for trailing \. Note that fgets includes the linefeed. */
3987c478bdstevel@tonic-gate	if (got >= 2 && line[got-2] == '\\') {
3997c478bdstevel@tonic-gate		/* Skip \ and LF */
4007c478bdstevel@tonic-gate		line += got - 2;
4017c478bdstevel@tonic-gate		length -= got - 2;
4027c478bdstevel@tonic-gate		goto retry;
4037c478bdstevel@tonic-gate	}
4047c478bdstevel@tonic-gate	/* Remove the trailing linefeed */
4057c478bdstevel@tonic-gate	if (got > 0)
4067c478bdstevel@tonic-gate		line[got-1] = '\0';
4077c478bdstevel@tonic-gate
4087c478bdstevel@tonic-gate	return (1);
4097c478bdstevel@tonic-gate}
4107c478bdstevel@tonic-gate
4117c478bdstevel@tonic-gate/*
4127c478bdstevel@tonic-gate * Parse a line splitting it off at whitspace characters.
4137c478bdstevel@tonic-gate * Modifies the content of the string by inserting NULLs.
4147c478bdstevel@tonic-gate * If more arguments than fits in argvec/argcount then ignore the last.
4157c478bdstevel@tonic-gate * Returns argcount.
4167c478bdstevel@tonic-gate * Handles single quotes and double quotes.
4177c478bdstevel@tonic-gate */
4187c478bdstevel@tonic-gatestatic int
4197c478bdstevel@tonic-gateparse_line(char *line, char *argvec[], int argcount)
4207c478bdstevel@tonic-gate{
4217c478bdstevel@tonic-gate	int i = 0;
4227c478bdstevel@tonic-gate	char *cp;
4237c478bdstevel@tonic-gate	boolean_t insingle_quote = _B_FALSE;
4247c478bdstevel@tonic-gate	boolean_t indouble_quote = _B_FALSE;
4257c478bdstevel@tonic-gate
4267c478bdstevel@tonic-gate	/* Truncate at the beginning of a comment */
4277c478bdstevel@tonic-gate	cp = strchr(line, '#');
4287c478bdstevel@tonic-gate	if (cp != NULL)
4297c478bdstevel@tonic-gate		*cp = '\0';
4307c478bdstevel@tonic-gate
4317c478bdstevel@tonic-gate	for (;;) {
4327c478bdstevel@tonic-gate		/* Skip any whitespace */
4337c478bdstevel@tonic-gate		while (isspace(*line) && *line != '\0')
4347c478bdstevel@tonic-gate			line++;
4357c478bdstevel@tonic-gate
4367c478bdstevel@tonic-gate		if (*line == '\'') {
4377c478bdstevel@tonic-gate			line++;
4387c478bdstevel@tonic-gate			if (*line == '\0')
4397c478bdstevel@tonic-gate				return (i);
4407c478bdstevel@tonic-gate			insingle_quote = _B_TRUE;
4417c478bdstevel@tonic-gate		} else if (*line == '"') {
4427c478bdstevel@tonic-gate			line++;
4437c478bdstevel@tonic-gate			if (*line == '\0')
4447c478bdstevel@tonic-gate				return (i);
4457c478bdstevel@tonic-gate			indouble_quote = _B_TRUE;
4467c478bdstevel@tonic-gate		}
4477c478bdstevel@tonic-gate		argvec[i] = line;
4487c478bdstevel@tonic-gate		if (*line == '\0')
4497c478bdstevel@tonic-gate			return (i);
4507c478bdstevel@tonic-gate		i++;
4517c478bdstevel@tonic-gate		/* Skip until next whitespace or end of quoted text */
4527c478bdstevel@tonic-gate		if (insingle_quote) {
4537c478bdstevel@tonic-gate			while (*line != '\'' && *line != '\0')
4547c478bdstevel@tonic-gate				line++;
4557c478bdstevel@tonic-gate			if (*line == '\'') {
4567c478bdstevel@tonic-gate				*line = ' ';
4577c478bdstevel@tonic-gate			} else {
4587c478bdstevel@tonic-gate				/* Handle missing quote at end */
4597c478bdstevel@tonic-gate				i--;
4607c478bdstevel@tonic-gate				conferr("Missing end quote - ignoring <%s>\n",
4617c478bdstevel@tonic-gate				    argvec[i]);
4627c478bdstevel@tonic-gate				return (i);
4637c478bdstevel@tonic-gate			}
4647c478bdstevel@tonic-gate			insingle_quote = _B_FALSE;
4657c478bdstevel@tonic-gate		} else if (indouble_quote) {
4667c478bdstevel@tonic-gate			while (*line != '"' && *line != '\0')
4677c478bdstevel@tonic-gate				line++;
4687c478bdstevel@tonic-gate			if (*line == '"') {
4697c478bdstevel@tonic-gate				*line = ' ';
4707c478bdstevel@tonic-gate			} else {
4717c478bdstevel@tonic-gate				/* Handle missing quote at end */
4727c478bdstevel@tonic-gate				i--;
4737c478bdstevel@tonic-gate				conferr("Missing end quote - ignoring <%s>\n",
4747c478bdstevel@tonic-gate				    argvec[i]);
4757c478bdstevel@tonic-gate				return (i);
4767c478bdstevel@tonic-gate			}
4777c478bdstevel@tonic-gate			indouble_quote = _B_FALSE;
4787c478bdstevel@tonic-gate		} else {
4797c478bdstevel@tonic-gate			while (!isspace(*line) && *line != '\0')
4807c478bdstevel@tonic-gate				line++;
4817c478bdstevel@tonic-gate		}
4827c478bdstevel@tonic-gate		if (*line != '\0') {
4837c478bdstevel@tonic-gate			/* Break off argument */
4847c478bdstevel@tonic-gate			*line++ = '\0';
4857c478bdstevel@tonic-gate		}
4867c478bdstevel@tonic-gate		if (i > argcount)
4877c478bdstevel@tonic-gate			return (argcount);
4887c478bdstevel@tonic-gate	}
4897c478bdstevel@tonic-gate	/* NOTREACHED */
4907c478bdstevel@tonic-gate}
4917c478bdstevel@tonic-gate
4927c478bdstevel@tonic-gatestatic void
4937c478bdstevel@tonic-gateparse_var_value(config_type_t type, struct configinfo *list, char *varstr,
4947c478bdstevel@tonic-gate    char *valstr, struct confvar *confvar)
4957c478bdstevel@tonic-gate{
4967c478bdstevel@tonic-gate	struct configinfo *cip;
4977c478bdstevel@tonic-gate	uint_t val;
4987c478bdstevel@tonic-gate
4997c478bdstevel@tonic-gate	if (debug & D_CONFIG) {
5007c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "parse_var_value(%d, %s, %s)\n",
5017c478bdstevel@tonic-gate		    (int)type, varstr, valstr);
5027c478bdstevel@tonic-gate	}
5037c478bdstevel@tonic-gate
5047c478bdstevel@tonic-gate	for (cip = list; cip->ci_name != NULL; cip++) {
5057c478bdstevel@tonic-gate		if (strcasecmp(cip->ci_name, varstr) == 0)
5067c478bdstevel@tonic-gate			break;
5077c478bdstevel@tonic-gate	}
5087c478bdstevel@tonic-gate	if (cip->ci_name == NULL) {
5097c478bdstevel@tonic-gate		conferr("Unknown variable: <%s>\n", varstr);
5107c478bdstevel@tonic-gate		return;
5117c478bdstevel@tonic-gate	}
5127c478bdstevel@tonic-gate	if (!(*cip->ci_parsefunc)(valstr, &val)) {
5137c478bdstevel@tonic-gate		conferr("Bad value: <%s>\n", valstr);
5147c478bdstevel@tonic-gate		return;
5157c478bdstevel@tonic-gate	}
5167c478bdstevel@tonic-gate	if (cip->ci_min != 0 && val < cip->ci_min) {
5177c478bdstevel@tonic-gate		conferr("Value %s is below minimum %u for %s\n",
5187c478bdstevel@tonic-gate		    valstr, cip->ci_min, varstr);
5197c478bdstevel@tonic-gate		return;
5207c478bdstevel@tonic-gate	}
5217c478bdstevel@tonic-gate	if (cip->ci_max != ~0U && val > cip->ci_max) {
5227c478bdstevel@tonic-gate		conferr("Value %s is above maximum %u for %s\n",
5237c478bdstevel@tonic-gate		    valstr, cip->ci_max, varstr);
5247c478bdstevel@tonic-gate		return;
5257c478bdstevel@tonic-gate	}
5267c478bdstevel@tonic-gate	/* Check against dynamic/relative limits */
5277c478bdstevel@tonic-gate	if (type == CONFIG_IF) {
5287c478bdstevel@tonic-gate		if (cip->ci_index == I_MinRtrAdvInterval &&
5297c478bdstevel@tonic-gate		    confvar[I_MaxRtrAdvInterval].cf_notdefault &&
5307c478bdstevel@tonic-gate		    val > confvar[I_MaxRtrAdvInterval].cf_value * 0.75) {
5317c478bdstevel@tonic-gate			conferr("MinRtrAdvInterval exceeds .75 * "
5327c478bdstevel@tonic-gate			    "MaxRtrAdvInterval (%u)\n",
5337c478bdstevel@tonic-gate			    confvar[I_MaxRtrAdvInterval].cf_value);
5347c478bdstevel@tonic-gate			return;
5357c478bdstevel@tonic-gate		}
5367c478bdstevel@tonic-gate		if (cip->ci_index == I_MaxRtrAdvInterval &&
5377c478bdstevel@tonic-gate		    confvar[I_MinRtrAdvInterval].cf_notdefault &&
5387c478bdstevel@tonic-gate		    confvar[I_MinRtrAdvInterval].cf_value > val * 0.75) {
5397c478bdstevel@tonic-gate			conferr("MinRtrAdvInterval (%u) exceeds .75 * "
5407c478bdstevel@tonic-gate			    "MaxRtrAdvInterval\n",
5417c478bdstevel@tonic-gate			    confvar[I_MinRtrAdvInterval].cf_value);
5427c478bdstevel@tonic-gate			return;
5437c478bdstevel@tonic-gate		}
5447c478bdstevel@tonic-gate		if (cip->ci_index == I_AdvDefaultLifetime &&
5457c478bdstevel@tonic-gate		    confvar[I_MaxRtrAdvInterval].cf_notdefault &&
5467c478bdstevel@tonic-gate		    val != 0 &&
5477c478bdstevel@tonic-gate		    val < confvar[I_MaxRtrAdvInterval].cf_value) {
5487c478bdstevel@tonic-gate			conferr("AdvDefaultLifetime is not between "
5497c478bdstevel@tonic-gate			    "MaxRtrAdrInterval (%u) and 9000 seconds\n",
5507c478bdstevel@tonic-gate			    confvar[I_MaxRtrAdvInterval].cf_value);
5517c478bdstevel@tonic-gate			return;
5527c478bdstevel@tonic-gate		}
5537c478bdstevel@tonic-gate		if (cip->ci_index == I_MaxRtrAdvInterval &&
5547c478bdstevel@tonic-gate		    confvar[I_AdvDefaultLifetime].cf_notdefault &&
5557c478bdstevel@tonic-gate		    confvar[I_AdvDefaultLifetime].cf_value < val) {
5567c478bdstevel@tonic-gate			conferr("AdvDefaultLifetime (%u) is not between "
5577c478bdstevel@tonic-gate			    "MaxRtrAdrInterval and 9000 seconds\n",
5587c478bdstevel@tonic-gate			    confvar[I_AdvDefaultLifetime].cf_value);
5597c478bdstevel@tonic-gate			return;
5607c478bdstevel@tonic-gate		}
5617c478bdstevel@tonic-gate	}
5627c478bdstevel@tonic-gate	confvar[cip->ci_index].cf_value = val;
5637c478bdstevel@tonic-gate	confvar[cip->ci_index].cf_notdefault = _B_TRUE;
5647c478bdstevel@tonic-gate
5657c478bdstevel@tonic-gate	/* Derive dynamic/relative variables based on this one */
5667c478bdstevel@tonic-gate	if (type == CONFIG_IF) {
5677c478bdstevel@tonic-gate		if (cip->ci_index == I_MaxRtrAdvInterval &&
5687c478bdstevel@tonic-gate		    !confvar[I_MinRtrAdvInterval].cf_notdefault)
5697c478bdstevel@tonic-gate			confvar[I_MinRtrAdvInterval].cf_value = val / 3;
5707c478bdstevel@tonic-gate		if (cip->ci_index == I_MaxRtrAdvInterval &&
5717c478bdstevel@tonic-gate		    !confvar[I_AdvDefaultLifetime].cf_notdefault)
5727c478bdstevel@tonic-gate		    confvar[I_AdvDefaultLifetime].cf_value = 3 * val;
5737c478bdstevel@tonic-gate	}
5747c478bdstevel@tonic-gate}
5757c478bdstevel@tonic-gate
5767c478bdstevel@tonic-gate/*
5777c478bdstevel@tonic-gate * Split up the line into <variable> <value> pairs
5787c478bdstevel@tonic-gate */
5797c478bdstevel@tonic-gatestatic void
5807c478bdstevel@tonic-gateparse_default(config_type_t type, struct configinfo *list,
5817c478bdstevel@tonic-gate    char *argvec[], int argcount, struct confvar *defaults)
5827c478bdstevel@tonic-gate{
5837c478bdstevel@tonic-gate	if (debug & D_CONFIG)
5847c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "parse_default: argc %d\n", argcount);
5857c478bdstevel@tonic-gate	while (argcount >= 2) {
5867c478bdstevel@tonic-gate		parse_var_value(type, list, argvec[0], argvec[1], defaults);
5877c478bdstevel@tonic-gate
5887c478bdstevel@tonic-gate		argcount -= 2;
5897c478bdstevel@tonic-gate		argvec += 2;
5907c478bdstevel@tonic-gate	}
5917c478bdstevel@tonic-gate	if (argcount != 0)
5927c478bdstevel@tonic-gate		conferr("Trailing text <%s> ignored\n", argvec[0]);
5937c478bdstevel@tonic-gate}
5947c478bdstevel@tonic-gate
5957c478bdstevel@tonic-gate/*
5967c478bdstevel@tonic-gate * Returns true if ok; otherwise false.
5977c478bdstevel@tonic-gate */
5987c478bdstevel@tonic-gatestatic void
5997c478bdstevel@tonic-gateparse_if(struct configinfo *list, char *argvec[], int argcount)
6007c478bdstevel@tonic-gate{
6017c478bdstevel@tonic-gate	char *ifname;
6027c478bdstevel@tonic-gate	struct phyint *pi;
6037c478bdstevel@tonic-gate	char save[sizeof (pi->pi_config)];
6047c478bdstevel@tonic-gate
6057c478bdstevel@tonic-gate	if (debug & D_CONFIG)
6067c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "parse_if: argc %d\n", argcount);
6077c478bdstevel@tonic-gate
6087c478bdstevel@tonic-gate	if (argcount < 1) {
6097c478bdstevel@tonic-gate		conferr("Missing interface name\n");
6107c478bdstevel@tonic-gate		return;
6117c478bdstevel@tonic-gate	}
6127c478bdstevel@tonic-gate	ifname = argvec[0];
6137c478bdstevel@tonic-gate	argvec++;
6147c478bdstevel@tonic-gate	argcount--;
6157c478bdstevel@tonic-gate
6167c478bdstevel@tonic-gate	pi = phyint_lookup(ifname);
6177c478bdstevel@tonic-gate	if (pi == NULL) {
6187c478bdstevel@tonic-gate		/*
6197c478bdstevel@tonic-gate		 * Create the physical interface structure.
6207c478bdstevel@tonic-gate		 * Note, phyint_create() sets the interface
6217c478bdstevel@tonic-gate		 * defaults in pi_config.
6227c478bdstevel@tonic-gate		 */
6237c478bdstevel@tonic-gate		pi = phyint_create(ifname);
6247c478bdstevel@tonic-gate		if (pi == NULL) {
6257c478bdstevel@tonic-gate			conferr("Unable to use interface %s\n", ifname);
6267c478bdstevel@tonic-gate			return;
6277c478bdstevel@tonic-gate		}
6287c478bdstevel@tonic-gate	}
6297c478bdstevel@tonic-gate
6307c478bdstevel@tonic-gate	(void) memcpy(save, pi->pi_config, sizeof (save));
6317c478bdstevel@tonic-gate	while (argcount >= 2) {
6327c478bdstevel@tonic-gate		parse_var_value(CONFIG_IF, list, argvec[0], argvec[1],
6337c478bdstevel@tonic-gate		    pi->pi_config);
6347c478bdstevel@tonic-gate
6357c478bdstevel@tonic-gate		argcount -= 2;
6367c478bdstevel@tonic-gate		argvec += 2;
6377c478bdstevel@tonic-gate	}
6387c478bdstevel@tonic-gate	if (argcount != 0)
6397c478bdstevel@tonic-gate		logmsg(LOG_ERR, "Trailing text <%s> ignored\n", argvec[0]);
6407c478bdstevel@tonic-gate	check_if_var_consistency(pi->pi_config, save, sizeof (save));
6417c478bdstevel@tonic-gate}
6427c478bdstevel@tonic-gate
6437c478bdstevel@tonic-gatestatic void
6447c478bdstevel@tonic-gateparse_prefix(struct configinfo *list, char *argvec[], int argcount)
6457c478bdstevel@tonic-gate{
6467c478bdstevel@tonic-gate	char *ifname, *prefix;
6477c478bdstevel@tonic-gate	struct phyint *pi;
6487c478bdstevel@tonic-gate	struct adv_prefix *adv_pr;
6497c478bdstevel@tonic-gate	struct in6_addr in6;
6507c478bdstevel@tonic-gate	int prefixlen;
6517c478bdstevel@tonic-gate	char save[sizeof (adv_pr->adv_pr_config)];
6527c478bdstevel@tonic-gate
6537c478bdstevel@tonic-gate	if (debug & D_CONFIG)
6547c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "parse_prefix: argc %d\n", argcount);
6557c478bdstevel@tonic-gate
6567c478bdstevel@tonic-gate	if (argcount < 2) {
6577c478bdstevel@tonic-gate		conferr("Missing prefix and/or interface name\n");
6587c478bdstevel@tonic-gate		return;
6597c478bdstevel@tonic-gate	}
6607c478bdstevel@tonic-gate	prefix = argvec[0];
6617c478bdstevel@tonic-gate	ifname = argvec[1];
6627c478bdstevel@tonic-gate	argvec += 2;
6637c478bdstevel@tonic-gate	argcount -= 2;
6647c478bdstevel@tonic-gate
6657c478bdstevel@tonic-gate	prefixlen = parse_addrprefix(prefix, &in6);
6667c478bdstevel@tonic-gate	if (prefixlen == -1) {
6677c478bdstevel@tonic-gate		conferr("Bad prefix %s\n", prefix);
6687c478bdstevel@tonic-gate		return;
6697c478bdstevel@tonic-gate	}
6707c478bdstevel@tonic-gate
6717c478bdstevel@tonic-gate	pi = phyint_lookup(ifname);
6727c478bdstevel@tonic-gate	if (pi == NULL) {
6737c478bdstevel@tonic-gate		/*
6747c478bdstevel@tonic-gate		 * Create the physical interface structure.
6757c478bdstevel@tonic-gate		 * Note, phyint_create() sets the interface
6767c478bdstevel@tonic-gate		 * defaults in pi_config.
6777c478bdstevel@tonic-gate		 */
6787c478bdstevel@tonic-gate		pi = phyint_create(ifname);
6797c478bdstevel@tonic-gate		if (pi == NULL) {
6807c478bdstevel@tonic-gate			conferr("Unable to use interface %s\n", ifname);
6817c478bdstevel@tonic-gate			return;
6827c478bdstevel@tonic-gate		}
6837c478bdstevel@tonic-gate	}
6847c478bdstevel@tonic-gate	adv_pr = adv_prefix_lookup(pi, in6, prefixlen);
6857c478bdstevel@tonic-gate	if (adv_pr == NULL) {
6867c478bdstevel@tonic-gate		int i;
6877c478bdstevel@tonic-gate
6887c478bdstevel@tonic-gate		adv_pr = adv_prefix_create(pi, in6, prefixlen);
6897c478bdstevel@tonic-gate		if (adv_pr == NULL) {
6907c478bdstevel@tonic-gate			conferr("Unable to create prefix %s\n", prefix);
6917c478bdstevel@tonic-gate			return;
6927c478bdstevel@tonic-gate		}
6937c478bdstevel@tonic-gate		/*
6947c478bdstevel@tonic-gate		 * Copy the defaults from the default array.
6957c478bdstevel@tonic-gate		 */
6967c478bdstevel@tonic-gate		for (i = 0; i < I_PREFIXSIZE; i++) {
6977c478bdstevel@tonic-gate			adv_pr->adv_pr_config[i].cf_value =
6987c478bdstevel@tonic-gate			    prefixdefaults[i].cf_value;
6997c478bdstevel@tonic-gate			adv_pr->adv_pr_config[i].cf_notdefault =
7007c478bdstevel@tonic-gate			    prefixdefaults[i].cf_notdefault;
7017c478bdstevel@tonic-gate		}
7027c478bdstevel@tonic-gate	}
7037c478bdstevel@tonic-gate
7047c478bdstevel@tonic-gate	(void) memcpy(save, adv_pr->adv_pr_config, sizeof (save));
7057c478bdstevel@tonic-gate	while (argcount >= 2) {
7067c478bdstevel@tonic-gate		parse_var_value(CONFIG_PREFIX, list, argvec[0], argvec[1],
7077c478bdstevel@tonic-gate		    adv_pr->adv_pr_config);
7087c478bdstevel@tonic-gate
7097c478bdstevel@tonic-gate		argcount -= 2;
7107c478bdstevel@tonic-gate		argvec += 2;
7117c478bdstevel@tonic-gate	}
7127c478bdstevel@tonic-gate	check_var_consistency(adv_pr->adv_pr_config, save, sizeof (save));
7137c478bdstevel@tonic-gate	if (argcount != 0)
7147c478bdstevel@tonic-gate		logmsg(LOG_ERR, "Trailing text <%s> ignored\n", argvec[0]);
7157c478bdstevel@tonic-gate}
7167c478bdstevel@tonic-gate
7177c478bdstevel@tonic-gate/*
7187c478bdstevel@tonic-gate * Returns true if ok (and *resp updated) and false if failed.
7197c478bdstevel@tonic-gate */
7207c478bdstevel@tonic-gatestatic boolean_t
7217c478bdstevel@tonic-gateparse_onoff(char *str, uint_t *resp)
7227c478bdstevel@tonic-gate{
7237c478bdstevel@tonic-gate	if (strcasecmp(str, "on") == 0) {
7247c478bdstevel@tonic-gate		*resp = 1;
7257c478bdstevel@tonic-gate		return (_B_TRUE);
7267c478bdstevel@tonic-gate	}
7277c478bdstevel@tonic-gate	if (strcasecmp(str, "off") == 0) {
7287c478bdstevel@tonic-gate		*resp = 0;
7297c478bdstevel@tonic-gate		return (_B_TRUE);
7307c478bdstevel@tonic-gate	}
7317c478bdstevel@tonic-gate	if (strcasecmp(str, "true") == 0) {
7327c478bdstevel@tonic-gate		*resp = 1;
7337c478bdstevel@tonic-gate		return (_B_TRUE);
7347c478bdstevel@tonic-gate	}
7357c478bdstevel@tonic-gate	if (strcasecmp(str, "false") == 0) {
7367c478bdstevel@tonic-gate		*resp = 0;
7377c478bdstevel@tonic-gate		return (_B_TRUE);
7387c478bdstevel@tonic-gate	}
7397c478bdstevel@tonic-gate	if (parse_int(str, resp)) {
7407c478bdstevel@tonic-gate		if (*resp == 0 || *resp == 1)
7417c478bdstevel@tonic-gate			return (_B_TRUE);
7427c478bdstevel@tonic-gate	}
7437c478bdstevel@tonic-gate	return (_B_FALSE);
7447c478bdstevel@tonic-gate}
7457c478bdstevel@tonic-gate
7467c478bdstevel@tonic-gate/*
7477c478bdstevel@tonic-gate * Returns true if ok (and *resp updated) and false if failed.
7487c478bdstevel@tonic-gate */
7497c478bdstevel@tonic-gatestatic boolean_t
7507c478bdstevel@tonic-gateparse_int(char *str, uint_t *resp)
7517c478bdstevel@tonic-gate{
7527c478bdstevel@tonic-gate	char *end;
7537c478bdstevel@tonic-gate	int res;
7547c478bdstevel@tonic-gate
7557c478bdstevel@tonic-gate	res = strtoul(str, &end, 0);
7567c478bdstevel@tonic-gate	if (end == str)
7577c478bdstevel@tonic-gate		return (_B_FALSE);
7587c478bdstevel@tonic-gate	*resp = res;
7597c478bdstevel@tonic-gate	return (_B_TRUE);
7607c478bdstevel@tonic-gate}
7617c478bdstevel@tonic-gate
7627c478bdstevel@tonic-gate/*
7637c478bdstevel@tonic-gate * Parse something with a unit of millseconds.
7647c478bdstevel@tonic-gate * Regognizes the suffixes "ms", "s", "m", "h", and "d".
7657c478bdstevel@tonic-gate *
7667c478bdstevel@tonic-gate * Returns true if ok (and *resp updated) and false if failed.
7677c478bdstevel@tonic-gate */
7687c478bdstevel@tonic-gatestatic boolean_t
7697c478bdstevel@tonic-gateparse_ms(char *str, uint_t *resp)
7707c478bdstevel@tonic-gate{
7717c478bdstevel@tonic-gate	/* Look at the last and next to last character */
7727c478bdstevel@tonic-gate	char *cp, *last, *nlast;
7737c478bdstevel@tonic-gate	char str2[BUFSIZ];	/* For local modification */
7747c478bdstevel@tonic-gate	int multiplier = 1;
7757c478bdstevel@tonic-gate
7767c478bdstevel@tonic-gate	(void) strncpy(str2, str, sizeof (str2));
7777c478bdstevel@tonic-gate	str2[sizeof (str2) - 1] = '\0';
7787c478bdstevel@tonic-gate
7797c478bdstevel@tonic-gate	last = str2;
7807c478bdstevel@tonic-gate	nlast = NULL;
7817c478bdstevel@tonic-gate	for (cp = str2; *cp != '\0'; cp++) {
7827c478bdstevel@tonic-gate		nlast = last;
7837c478bdstevel@tonic-gate		last = cp;
7847c478bdstevel@tonic-gate	}
7857c478bdstevel@tonic-gate	if (debug & D_PARSE) {
7867c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "parse_ms: last <%c> nlast <%c>\n",
7877c478bdstevel@tonic-gate		    (last != NULL ? *last : ' '),
7887c478bdstevel@tonic-gate		    (nlast != NULL ? *nlast : ' '));
7897c478bdstevel@tonic-gate	}
7907c478bdstevel@tonic-gate	switch (*last) {
7917c478bdstevel@tonic-gate	case 'd':
7927c478bdstevel@tonic-gate		multiplier *= 24;
7937c478bdstevel@tonic-gate		/* FALLTHRU */
7947c478bdstevel@tonic-gate	case 'h':
7957c478bdstevel@tonic-gate		multiplier *= 60;
7967c478bdstevel@tonic-gate		/* FALLTHRU */
7977c478bdstevel@tonic-gate	case 'm':
7987c478bdstevel@tonic-gate		multiplier *= 60;
7997c478bdstevel@tonic-gate		*last = '\0';
8007c478bdstevel@tonic-gate		multiplier *= 1000;	/* Convert to milliseconds */
8017c478bdstevel@tonic-gate		break;
8027c478bdstevel@tonic-gate	case 's':
8037c478bdstevel@tonic-gate		/* Could be "ms" or "s" */
8047c478bdstevel@tonic-gate		if (nlast != NULL && *nlast == 'm') {
8057c478bdstevel@tonic-gate			/* "ms" */
8067c478bdstevel@tonic-gate			*nlast = '\0';
8077c478bdstevel@tonic-gate		} else {
8087c478bdstevel@tonic-gate			*last = '\0';
8097c478bdstevel@tonic-gate			multiplier *= 1000;	/* Convert to milliseconds */
8107c478bdstevel@tonic-gate		}
8117c478bdstevel@tonic-gate		break;
8127c478bdstevel@tonic-gate	}
8137c478bdstevel@tonic-gate
8147c478bdstevel@tonic-gate	if (!parse_int(str2, resp))
8157c478bdstevel@tonic-gate		return (_B_FALSE);
8167c478bdstevel@tonic-gate
8177c478bdstevel@tonic-gate	*resp *= multiplier;
8187c478bdstevel@tonic-gate	return (_B_TRUE);
8197c478bdstevel@tonic-gate}
8207c478bdstevel@tonic-gate
8217c478bdstevel@tonic-gate/*
8227c478bdstevel@tonic-gate * Parse something with a unit of seconds.
8237c478bdstevel@tonic-gate * Regognizes the suffixes "s", "m", "h", and "d".
8247c478bdstevel@tonic-gate *
8257c478bdstevel@tonic-gate * Returns true if ok (and *resp updated) and false if failed.
8267c478bdstevel@tonic-gate */
8277c478bdstevel@tonic-gatestatic boolean_t
8287c478bdstevel@tonic-gateparse_s(char *str, uint_t *resp)
8297c478bdstevel@tonic-gate{
8307c478bdstevel@tonic-gate	/* Look at the last character */
8317c478bdstevel@tonic-gate	char *cp, *last;
8327c478bdstevel@tonic-gate	char str2[BUFSIZ];	/* For local modification */
8337c478bdstevel@tonic-gate	int multiplier = 1;
8347c478bdstevel@tonic-gate
8357c478bdstevel@tonic-gate	(void) strncpy(str2, str, sizeof (str2));
8367c478bdstevel@tonic-gate	str2[sizeof (str2) - 1] = '\0';
8377c478bdstevel@tonic-gate
8387c478bdstevel@tonic-gate	last = str2;
8397c478bdstevel@tonic-gate	for (cp = str2; *cp != '\0'; cp++) {
8407c478bdstevel@tonic-gate		last = cp;
8417c478bdstevel@tonic-gate	}
8427c478bdstevel@tonic-gate	if (debug & D_PARSE) {
8437c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "parse_s: last <%c>\n",
8447c478bdstevel@tonic-gate		    (last != NULL ? *last : ' '));
8457c478bdstevel@tonic-gate	}
8467c478bdstevel@tonic-gate	switch (*last) {
8477c478bdstevel@tonic-gate	case 'd':
8487c478bdstevel@tonic-gate		multiplier *= 24;
8497c478bdstevel@tonic-gate		/* FALLTHRU */
8507c478bdstevel@tonic-gate	case 'h':
8517c478bdstevel@tonic-gate		multiplier *= 60;
8527c478bdstevel@tonic-gate		/* FALLTHRU */
8537c478bdstevel@tonic-gate	case 'm':
8547c478bdstevel@tonic-gate		multiplier *= 60;
8557c478bdstevel@tonic-gate		/* FALLTHRU */
8567c478bdstevel@tonic-gate	case 's':
8577c478bdstevel@tonic-gate		*last = '\0';
8587c478bdstevel@tonic-gate		break;
8597c478bdstevel@tonic-gate	}
8607c478bdstevel@tonic-gate	if (!parse_int(str2, resp))
8617c478bdstevel@tonic-gate		return (_B_FALSE);
8627c478bdstevel@tonic-gate
8637c478bdstevel@tonic-gate	*resp *= multiplier;
8647c478bdstevel@tonic-gate	return (_B_TRUE);
8657c478bdstevel@tonic-gate}
8667c478bdstevel@tonic-gate
8677c478bdstevel@tonic-gate/*
8687c478bdstevel@tonic-gate * Return prefixlen (0 to 128) if ok; -1 if failed.
8697c478bdstevel@tonic-gate */
8707c478bdstevel@tonic-gatestatic int
8717c478bdstevel@tonic-gateparse_addrprefix(char *strin, struct in6_addr *in6)
8727c478bdstevel@tonic-gate{
8737c478bdstevel@tonic-gate	char str[BUFSIZ];	/* Local copy for modification */
8747c478bdstevel@tonic-gate	int prefixlen;
8757c478bdstevel@tonic-gate	char *cp;
8767c478bdstevel@tonic-gate	char *end;
8777c478bdstevel@tonic-gate
8787c478bdstevel@tonic-gate	(void) strncpy(str, strin, sizeof (str));
8797c478bdstevel@tonic-gate	str[sizeof (str) - 1] = '\0';
8807c478bdstevel@tonic-gate
8817c478bdstevel@tonic-gate	cp = strchr(str, '/');
8827c478bdstevel@tonic-gate	if (cp == NULL)
8837c478bdstevel@tonic-gate		return (-1);
8847c478bdstevel@tonic-gate	*cp = '\0';
8857c478bdstevel@tonic-gate	cp++;
8867c478bdstevel@tonic-gate
8877c478bdstevel@tonic-gate	prefixlen = strtol(cp, &end, 10);
8887c478bdstevel@tonic-gate	if (cp == end)
8897c478bdstevel@tonic-gate		return (-1);
8907c478bdstevel@tonic-gate
8917c478bdstevel@tonic-gate	if (prefixlen < 0 || prefixlen > IPV6_ABITS)
8927c478bdstevel@tonic-gate		return (-1);
8937c478bdstevel@tonic-gate
8947c478bdstevel@tonic-gate	if (inet_pton(AF_INET6, str, in6) != 1)
8957c478bdstevel@tonic-gate		return (-1);
8967c478bdstevel@tonic-gate
8977c478bdstevel@tonic-gate	return (prefixlen);
8987c478bdstevel@tonic-gate}
8997c478bdstevel@tonic-gate
9007c478bdstevel@tonic-gate/*
9017c478bdstevel@tonic-gate * Parse an absolute date using a datemsk config file.
9027c478bdstevel@tonic-gate * Return the difference (measured in seconds) between that date/time and
9037c478bdstevel@tonic-gate * the current date/time.
9047c478bdstevel@tonic-gate * If the date has passed return zero.
9057c478bdstevel@tonic-gate *
9067c478bdstevel@tonic-gate * Returns true if ok (and *resp updated) and false if failed.
9077c478bdstevel@tonic-gate * XXX Due to getdate limitations can not exceed year 2038.
9087c478bdstevel@tonic-gate */
9097c478bdstevel@tonic-gatestatic boolean_t
9107c478bdstevel@tonic-gateparse_date(char *str, uint_t *resp)
9117c478bdstevel@tonic-gate{
9127c478bdstevel@tonic-gate	struct tm *tm;
9137c478bdstevel@tonic-gate	struct timeval tvs;
9147c478bdstevel@tonic-gate	time_t time, ntime;
9157c478bdstevel@tonic-gate
9167c478bdstevel@tonic-gate	if (getenv("DATEMSK") == NULL) {
9177c478bdstevel@tonic-gate		(void) putenv("DATEMSK=/etc/inet/datemsk.ndpd");
9187c478bdstevel@tonic-gate	}
9197c478bdstevel@tonic-gate
9207c478bdstevel@tonic-gate	if (gettimeofday(&tvs, NULL) < 0) {
9217c478bdstevel@tonic-gate		logperror("gettimeofday");
9227c478bdstevel@tonic-gate		return (_B_FALSE);
9237c478bdstevel@tonic-gate	}
9247c478bdstevel@tonic-gate	time = tvs.tv_sec;
9257c478bdstevel@tonic-gate	tm = getdate(str);
9267c478bdstevel@tonic-gate	if (tm == NULL) {
9277c478bdstevel@tonic-gate		logmsg(LOG_ERR, "Bad date <%s> (error %d)\n",
9287c478bdstevel@tonic-gate		    str, getdate_err);
9297c478bdstevel@tonic-gate		return (_B_FALSE);
9307c478bdstevel@tonic-gate	}
9317c478bdstevel@tonic-gate
9327c478bdstevel@tonic-gate	ntime = mktime(tm);
9337c478bdstevel@tonic-gate
9347c478bdstevel@tonic-gate	if (debug & D_PARSE) {
9357c478bdstevel@tonic-gate		char buf[BUFSIZ];
9367c478bdstevel@tonic-gate
9377c478bdstevel@tonic-gate		(void) strftime(buf, sizeof (buf), "%Y-%m-%d %R %Z", tm);
9387c478bdstevel@tonic-gate		logmsg(LOG_DEBUG, "parse_date: <%s>, delta %ld seconds\n",
9397c478bdstevel@tonic-gate		    buf, ntime - time);
9407c478bdstevel@tonic-gate	}
9417c478bdstevel@tonic-gate	if (ntime < time) {
9427c478bdstevel@tonic-gate		conferr("Date in the past <%s>\n", str);
9437c478bdstevel@tonic-gate		*resp = 0;
9447c478bdstevel@tonic-gate		return (_B_TRUE);
9457c478bdstevel@tonic-gate	}
9467c478bdstevel@tonic-gate	*resp = (ntime - time);
9477c478bdstevel@tonic-gate	return (_B_TRUE);
9487c478bdstevel@tonic-gate}
9497c478bdstevel@tonic-gate
9507c478bdstevel@tonic-gate/* PRINTFLIKE1 */
9517c478bdstevel@tonic-gatestatic void
9527c478bdstevel@tonic-gateconferr(char *fmt, ...)
9537c478bdstevel@tonic-gate{
9547c478bdstevel@tonic-gate	char msg[NDPD_LOGMSGSIZE];
9557c478bdstevel@tonic-gate	size_t slen;
9567c478bdstevel@tonic-gate
9577c478bdstevel@tonic-gate	va_list ap;
9587c478bdstevel@tonic-gate	va_start(ap, fmt);
9597c478bdstevel@tonic-gate
9607c478bdstevel@tonic-gate	(void) snprintf(msg, NDPD_LOGMSGSIZE, "%s line %d: ",
9617c478bdstevel@tonic-gate	    conf_filename, lineno);
9627c478bdstevel@tonic-gate	slen = strlen(msg);
9637c478bdstevel@tonic-gate	(void) vsnprintf(msg + slen, NDPD_LOGMSGSIZE - slen, fmt, ap);
9647c478bdstevel@tonic-gate
9657c478bdstevel@tonic-gate	logmsg(LOG_ERR, "%s", msg);
9667c478bdstevel@tonic-gate
9677c478bdstevel@tonic-gate	va_end(ap);
9687c478bdstevel@tonic-gate}
9697c478bdstevel@tonic-gate
9707c478bdstevel@tonic-gatestatic FILE *
9717c478bdstevel@tonic-gateopen_conffile(char *filename)
9727c478bdstevel@tonic-gate{
9737c478bdstevel@tonic-gate	if (strlcpy(conf_filename, filename, MAXPATHLEN) >= MAXPATHLEN) {
9747c478bdstevel@tonic-gate		logmsg(LOG_ERR, "config file pathname is too long\n");
9757c478bdstevel@tonic-gate		return (NULL);
9767c478bdstevel@tonic-gate	}
9777c478bdstevel@tonic-gate
9787c478bdstevel@tonic-gate	lineno = 0;
9797c478bdstevel@tonic-gate
9807c478bdstevel@tonic-gate	return (fopen(filename, "r"));
9817c478bdstevel@tonic-gate
9827c478bdstevel@tonic-gate}
983