xref: /illumos-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c (revision a1196271e1d6e6ce9cc40c8b94f5b659935e82bc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
569bb4bb4Scarlsonj  * Common Development and Distribution License (the "License").
669bb4bb4Scarlsonj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a1196271SJames Carlson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <string.h>
297c478bd9Sstevel@tonic-gate #include <ctype.h>
307c478bd9Sstevel@tonic-gate #include <dhcpmsg.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <libnvpair.h>
347c478bd9Sstevel@tonic-gate 
35d04ccbb3Scarlsonj #include "common.h"
367c478bd9Sstevel@tonic-gate #include "defaults.h"
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate struct dhcp_default {
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	const char	*df_name;	/* parameter name */
417c478bd9Sstevel@tonic-gate 	const char	*df_default;	/* default value */
427c478bd9Sstevel@tonic-gate 	int		df_min;		/* min value if type DF_INTEGER */
437c478bd9Sstevel@tonic-gate 	int		df_max;		/* max value if type DF_INTEGER */
447c478bd9Sstevel@tonic-gate };
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * note: keep in the same order as tunable parameter constants in defaults.h
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static struct dhcp_default defaults[] = {
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	{ "RELEASE_ON_SIGTERM",  "0",	 0,   0	  },
5369bb4bb4Scarlsonj 	{ "IGNORE_FAILED_ARP",	 "1",	 0,   -1  },
547c478bd9Sstevel@tonic-gate 	{ "OFFER_WAIT",		 "3",	 1,   20  },
5569bb4bb4Scarlsonj 	{ "ARP_WAIT",		 "1000", 0,   -1  },
567c478bd9Sstevel@tonic-gate 	{ "CLIENT_ID",		 NULL,	 0,   0	  },
577c478bd9Sstevel@tonic-gate 	{ "PARAM_REQUEST_LIST",  NULL,	 0,   0   },
58d04ccbb3Scarlsonj 	{ "REQUEST_HOSTNAME",	 "1",	 0,   0	  },
59d04ccbb3Scarlsonj 	{ "DEBUG_LEVEL",	 "0",	 0,   3   },
60*a1196271SJames Carlson 	{ "VERBOSE",		 "0",	 0,   0   },
61*a1196271SJames Carlson 	{ "VERIFIED_LEASE_ONLY", "0",	 0,   0	  },
62*a1196271SJames Carlson 	{ "PARAM_IGNORE_LIST",	 NULL,	 0,   0   }
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * df_build_cache(): builds the defaults nvlist cache
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  *   input: void
697c478bd9Sstevel@tonic-gate  *  output: a pointer to an nvlist of the current defaults, or NULL on failure
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static nvlist_t *
737c478bd9Sstevel@tonic-gate df_build_cache(void)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	char		entry[1024];
767c478bd9Sstevel@tonic-gate 	int		i;
77d04ccbb3Scarlsonj 	char		*param, *pastv6, *value, *end;
787c478bd9Sstevel@tonic-gate 	FILE		*fp;
797c478bd9Sstevel@tonic-gate 	nvlist_t 	*nvlist;
8069bb4bb4Scarlsonj 	struct dhcp_default *defp;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
837c478bd9Sstevel@tonic-gate 		return (NULL);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
867c478bd9Sstevel@tonic-gate 		dhcpmsg(MSG_WARNING, "cannot build default value cache; "
877c478bd9Sstevel@tonic-gate 		    "using built-in defaults");
887c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
897c478bd9Sstevel@tonic-gate 		return (NULL);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	while (fgets(entry, sizeof (entry), fp) != NULL) {
937c478bd9Sstevel@tonic-gate 		for (i = 0; entry[i] == ' '; i++)
947c478bd9Sstevel@tonic-gate 			;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		end = strrchr(entry, '\n');
977c478bd9Sstevel@tonic-gate 		value = strchr(entry, '=');
987c478bd9Sstevel@tonic-gate 		if (end == NULL || value == NULL || entry[i] == '#')
997c478bd9Sstevel@tonic-gate 			continue;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 		*end = '\0';
1027c478bd9Sstevel@tonic-gate 		*value++ = '\0';
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		/*
1057c478bd9Sstevel@tonic-gate 		 * to be compatible with the old defread()-based code
1067c478bd9Sstevel@tonic-gate 		 * which ignored case, store the parameters (except for the
1077c478bd9Sstevel@tonic-gate 		 * leading interface name) in upper case.
1087c478bd9Sstevel@tonic-gate 		 */
1097c478bd9Sstevel@tonic-gate 
110d04ccbb3Scarlsonj 		if ((param = strchr(entry, '.')) == NULL) {
111d04ccbb3Scarlsonj 			pastv6 = param = entry;
112d04ccbb3Scarlsonj 		} else {
113d04ccbb3Scarlsonj 			pastv6 = ++param;
114d04ccbb3Scarlsonj 			if (strncasecmp(param, "v6.", 3) == 0)
115d04ccbb3Scarlsonj 				pastv6 += 3;
116d04ccbb3Scarlsonj 		}
1177c478bd9Sstevel@tonic-gate 
11869bb4bb4Scarlsonj 		for (defp = defaults;
11969bb4bb4Scarlsonj 		    (char *)defp < (char *)defaults + sizeof (defaults);
12069bb4bb4Scarlsonj 		    defp++) {
121d04ccbb3Scarlsonj 			if (strcasecmp(pastv6, defp->df_name) == 0) {
12269bb4bb4Scarlsonj 				if (defp->df_max == -1) {
12369bb4bb4Scarlsonj 					dhcpmsg(MSG_WARNING, "parameter %s is "
12469bb4bb4Scarlsonj 					    "obsolete; ignored", defp->df_name);
12569bb4bb4Scarlsonj 				}
12669bb4bb4Scarlsonj 				break;
12769bb4bb4Scarlsonj 			}
12869bb4bb4Scarlsonj 		}
12969bb4bb4Scarlsonj 
1307c478bd9Sstevel@tonic-gate 		for (; *param != '\0'; param++)
1317c478bd9Sstevel@tonic-gate 			*param = toupper(*param);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
1347c478bd9Sstevel@tonic-gate 			dhcpmsg(MSG_WARNING, "cannot build default value cache;"
1357c478bd9Sstevel@tonic-gate 			    " using built-in defaults");
1367c478bd9Sstevel@tonic-gate 			nvlist_free(nvlist);
1377c478bd9Sstevel@tonic-gate 			nvlist = NULL;
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
1437c478bd9Sstevel@tonic-gate 	return (nvlist);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /*
1477c478bd9Sstevel@tonic-gate  * df_get_string(): gets the string value of a given user-tunable parameter
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  *   input: const char *: the interface the parameter applies to
150d04ccbb3Scarlsonj  *	    boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
151d04ccbb3Scarlsonj  *	    uint_t: the parameter number to look up
1527c478bd9Sstevel@tonic-gate  *  output: const char *: the parameter's value, or default if not set
1537c478bd9Sstevel@tonic-gate  *			  (must be copied by caller to be kept)
1547c478bd9Sstevel@tonic-gate  *    NOTE: df_get_string() is both used by functions outside this source
1557c478bd9Sstevel@tonic-gate  *	    file to retrieve strings from the defaults file, *and*
1567c478bd9Sstevel@tonic-gate  *	    internally by other df_get_*() functions.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate const char *
160d04ccbb3Scarlsonj df_get_string(const char *if_name, boolean_t isv6, uint_t param)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	char			*value;
163d04ccbb3Scarlsonj 	char			paramstr[256];
164d04ccbb3Scarlsonj 	char			name[256];
1657c478bd9Sstevel@tonic-gate 	struct stat		statbuf;
1667c478bd9Sstevel@tonic-gate 	static struct stat	df_statbuf;
1677c478bd9Sstevel@tonic-gate 	static boolean_t	df_unavail_msg = B_FALSE;
1687c478bd9Sstevel@tonic-gate 	static nvlist_t		*df_nvlist = NULL;
1697c478bd9Sstevel@tonic-gate 
170d04ccbb3Scarlsonj 	if (param >= (sizeof (defaults) / sizeof (*defaults)))
1717c478bd9Sstevel@tonic-gate 		return (NULL);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
1747c478bd9Sstevel@tonic-gate 		if (!df_unavail_msg) {
1757c478bd9Sstevel@tonic-gate 			dhcpmsg(MSG_WARNING, "cannot access %s; using "
1767c478bd9Sstevel@tonic-gate 			    "built-in defaults", DHCP_AGENT_DEFAULTS);
1777c478bd9Sstevel@tonic-gate 			df_unavail_msg = B_TRUE;
1787c478bd9Sstevel@tonic-gate 		}
179d04ccbb3Scarlsonj 		return (defaults[param].df_default);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * if our cached parameters are stale, rebuild.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (statbuf.st_mtime != df_statbuf.st_mtime ||
1877c478bd9Sstevel@tonic-gate 	    statbuf.st_size != df_statbuf.st_size) {
1887c478bd9Sstevel@tonic-gate 		df_statbuf = statbuf;
1897c478bd9Sstevel@tonic-gate 		if (df_nvlist != NULL)
1907c478bd9Sstevel@tonic-gate 			nvlist_free(df_nvlist);
1917c478bd9Sstevel@tonic-gate 		df_nvlist = df_build_cache();
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
194d04ccbb3Scarlsonj 	if (isv6) {
195d04ccbb3Scarlsonj 		(void) snprintf(name, sizeof (name), ".V6.%s",
196d04ccbb3Scarlsonj 		    defaults[param].df_name);
197d04ccbb3Scarlsonj 		(void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
198d04ccbb3Scarlsonj 		    name);
199d04ccbb3Scarlsonj 	} else {
200d04ccbb3Scarlsonj 		(void) strlcpy(name, defaults[param].df_name, sizeof (name));
201d04ccbb3Scarlsonj 		(void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name,
202d04ccbb3Scarlsonj 		    name);
203d04ccbb3Scarlsonj 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
206d04ccbb3Scarlsonj 	 * first look for `if_name.[v6.]param', then `[v6.]param'.  if neither
2077c478bd9Sstevel@tonic-gate 	 * has been set, use the built-in default.
2087c478bd9Sstevel@tonic-gate 	 */
2097c478bd9Sstevel@tonic-gate 
210d04ccbb3Scarlsonj 	if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 ||
211d04ccbb3Scarlsonj 	    nvlist_lookup_string(df_nvlist, name, &value) == 0)
2127c478bd9Sstevel@tonic-gate 		return (value);
2137c478bd9Sstevel@tonic-gate 
214d04ccbb3Scarlsonj 	return (defaults[param].df_default);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * df_get_int(): gets the integer value of a given user-tunable parameter
2197c478bd9Sstevel@tonic-gate  *
2207c478bd9Sstevel@tonic-gate  *   input: const char *: the interface the parameter applies to
221d04ccbb3Scarlsonj  *	    boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
222d04ccbb3Scarlsonj  *	    uint_t: the parameter number to look up
2237c478bd9Sstevel@tonic-gate  *  output: int: the parameter's value, or default if not set
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate int
227d04ccbb3Scarlsonj df_get_int(const char *if_name, boolean_t isv6, uint_t param)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	const char	*value;
2307c478bd9Sstevel@tonic-gate 	int		value_int;
2317c478bd9Sstevel@tonic-gate 
232d04ccbb3Scarlsonj 	if (param >= (sizeof (defaults) / sizeof (*defaults)))
2337c478bd9Sstevel@tonic-gate 		return (0);
2347c478bd9Sstevel@tonic-gate 
235d04ccbb3Scarlsonj 	value = df_get_string(if_name, isv6, param);
2367c478bd9Sstevel@tonic-gate 	if (value == NULL || !isdigit(*value))
2377c478bd9Sstevel@tonic-gate 		goto failure;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	value_int = atoi(value);
240d04ccbb3Scarlsonj 	if (value_int > defaults[param].df_max ||
241d04ccbb3Scarlsonj 	    value_int < defaults[param].df_min)
2427c478bd9Sstevel@tonic-gate 		goto failure;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	return (value_int);
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate failure:
2477c478bd9Sstevel@tonic-gate 	dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
248d04ccbb3Scarlsonj 	    "%d, defaulting to `%s'", defaults[param].df_name,
249d04ccbb3Scarlsonj 	    defaults[param].df_min, defaults[param].df_max,
250d04ccbb3Scarlsonj 	    defaults[param].df_default);
251d04ccbb3Scarlsonj 	return (atoi(defaults[param].df_default));
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate  * df_get_bool(): gets the boolean value of a given user-tunable parameter
2567c478bd9Sstevel@tonic-gate  *
2577c478bd9Sstevel@tonic-gate  *   input: const char *: the interface the parameter applies to
258d04ccbb3Scarlsonj  *	    boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
259d04ccbb3Scarlsonj  *	    uint_t: the parameter number to look up
2607c478bd9Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate boolean_t
264d04ccbb3Scarlsonj df_get_bool(const char *if_name, boolean_t isv6, uint_t param)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	const char	*value;
2677c478bd9Sstevel@tonic-gate 
268d04ccbb3Scarlsonj 	if (param >= (sizeof (defaults) / sizeof (*defaults)))
2697c478bd9Sstevel@tonic-gate 		return (0);
2707c478bd9Sstevel@tonic-gate 
271d04ccbb3Scarlsonj 	value = df_get_string(if_name, isv6, param);
2727c478bd9Sstevel@tonic-gate 	if (value != NULL) {
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		if (strcasecmp(value, "true") == 0 ||
2757c478bd9Sstevel@tonic-gate 		    strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
2767c478bd9Sstevel@tonic-gate 			return (B_TRUE);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		if (strcasecmp(value, "false") == 0 ||
2797c478bd9Sstevel@tonic-gate 		    strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
2807c478bd9Sstevel@tonic-gate 			return (B_FALSE);
2817c478bd9Sstevel@tonic-gate 	}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
284d04ccbb3Scarlsonj 	    "`%s', defaulting to `%s'", defaults[param].df_name,
285d04ccbb3Scarlsonj 	    value != NULL ? value : "NULL", defaults[param].df_default);
2867c478bd9Sstevel@tonic-gate 
287d04ccbb3Scarlsonj 	return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE);
2887c478bd9Sstevel@tonic-gate }
289