10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52546Scarlsonj * Common Development and Distribution License (the "License").
62546Scarlsonj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9633Sjames.d.carlson@sun.com * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <string.h>
290Sstevel@tonic-gate #include <ctype.h>
300Sstevel@tonic-gate #include <dhcpmsg.h>
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <libnvpair.h>
340Sstevel@tonic-gate
353431Scarlsonj #include "common.h"
360Sstevel@tonic-gate #include "defaults.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate struct dhcp_default {
390Sstevel@tonic-gate
400Sstevel@tonic-gate const char *df_name; /* parameter name */
410Sstevel@tonic-gate const char *df_default; /* default value */
420Sstevel@tonic-gate int df_min; /* min value if type DF_INTEGER */
430Sstevel@tonic-gate int df_max; /* max value if type DF_INTEGER */
440Sstevel@tonic-gate };
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate * note: keep in the same order as tunable parameter constants in defaults.h
480Sstevel@tonic-gate */
490Sstevel@tonic-gate
500Sstevel@tonic-gate static struct dhcp_default defaults[] = {
510Sstevel@tonic-gate
520Sstevel@tonic-gate { "RELEASE_ON_SIGTERM", "0", 0, 0 },
532546Scarlsonj { "IGNORE_FAILED_ARP", "1", 0, -1 },
540Sstevel@tonic-gate { "OFFER_WAIT", "3", 1, 20 },
552546Scarlsonj { "ARP_WAIT", "1000", 0, -1 },
560Sstevel@tonic-gate { "CLIENT_ID", NULL, 0, 0 },
573431Scarlsonj { "PARAM_REQUEST_LIST", NULL, 0, 0 },
583431Scarlsonj { "REQUEST_HOSTNAME", "1", 0, 0 },
593431Scarlsonj { "DEBUG_LEVEL", "0", 0, 3 },
60*9633Sjames.d.carlson@sun.com { "VERBOSE", "0", 0, 0 },
61*9633Sjames.d.carlson@sun.com { "VERIFIED_LEASE_ONLY", "0", 0, 0 },
62*9633Sjames.d.carlson@sun.com { "PARAM_IGNORE_LIST", NULL, 0, 0 }
630Sstevel@tonic-gate };
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * df_build_cache(): builds the defaults nvlist cache
670Sstevel@tonic-gate *
680Sstevel@tonic-gate * input: void
690Sstevel@tonic-gate * output: a pointer to an nvlist of the current defaults, or NULL on failure
700Sstevel@tonic-gate */
710Sstevel@tonic-gate
720Sstevel@tonic-gate static nvlist_t *
df_build_cache(void)730Sstevel@tonic-gate df_build_cache(void)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate char entry[1024];
760Sstevel@tonic-gate int i;
773431Scarlsonj char *param, *pastv6, *value, *end;
780Sstevel@tonic-gate FILE *fp;
790Sstevel@tonic-gate nvlist_t *nvlist;
802546Scarlsonj struct dhcp_default *defp;
810Sstevel@tonic-gate
820Sstevel@tonic-gate if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
830Sstevel@tonic-gate return (NULL);
840Sstevel@tonic-gate
850Sstevel@tonic-gate if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
860Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot build default value cache; "
870Sstevel@tonic-gate "using built-in defaults");
880Sstevel@tonic-gate (void) fclose(fp);
890Sstevel@tonic-gate return (NULL);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate
920Sstevel@tonic-gate while (fgets(entry, sizeof (entry), fp) != NULL) {
930Sstevel@tonic-gate for (i = 0; entry[i] == ' '; i++)
940Sstevel@tonic-gate ;
950Sstevel@tonic-gate
960Sstevel@tonic-gate end = strrchr(entry, '\n');
970Sstevel@tonic-gate value = strchr(entry, '=');
980Sstevel@tonic-gate if (end == NULL || value == NULL || entry[i] == '#')
990Sstevel@tonic-gate continue;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate *end = '\0';
1020Sstevel@tonic-gate *value++ = '\0';
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /*
1050Sstevel@tonic-gate * to be compatible with the old defread()-based code
1060Sstevel@tonic-gate * which ignored case, store the parameters (except for the
1070Sstevel@tonic-gate * leading interface name) in upper case.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate
1103431Scarlsonj if ((param = strchr(entry, '.')) == NULL) {
1113431Scarlsonj pastv6 = param = entry;
1123431Scarlsonj } else {
1133431Scarlsonj pastv6 = ++param;
1143431Scarlsonj if (strncasecmp(param, "v6.", 3) == 0)
1153431Scarlsonj pastv6 += 3;
1163431Scarlsonj }
1170Sstevel@tonic-gate
1182546Scarlsonj for (defp = defaults;
1192546Scarlsonj (char *)defp < (char *)defaults + sizeof (defaults);
1202546Scarlsonj defp++) {
1213431Scarlsonj if (strcasecmp(pastv6, defp->df_name) == 0) {
1222546Scarlsonj if (defp->df_max == -1) {
1232546Scarlsonj dhcpmsg(MSG_WARNING, "parameter %s is "
1242546Scarlsonj "obsolete; ignored", defp->df_name);
1252546Scarlsonj }
1262546Scarlsonj break;
1272546Scarlsonj }
1282546Scarlsonj }
1292546Scarlsonj
1300Sstevel@tonic-gate for (; *param != '\0'; param++)
1310Sstevel@tonic-gate *param = toupper(*param);
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
1340Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot build default value cache;"
1350Sstevel@tonic-gate " using built-in defaults");
1360Sstevel@tonic-gate nvlist_free(nvlist);
1370Sstevel@tonic-gate nvlist = NULL;
1380Sstevel@tonic-gate break;
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate (void) fclose(fp);
1430Sstevel@tonic-gate return (nvlist);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate * df_get_string(): gets the string value of a given user-tunable parameter
1480Sstevel@tonic-gate *
1490Sstevel@tonic-gate * input: const char *: the interface the parameter applies to
1503431Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
1513431Scarlsonj * uint_t: the parameter number to look up
1520Sstevel@tonic-gate * output: const char *: the parameter's value, or default if not set
1530Sstevel@tonic-gate * (must be copied by caller to be kept)
1540Sstevel@tonic-gate * NOTE: df_get_string() is both used by functions outside this source
1550Sstevel@tonic-gate * file to retrieve strings from the defaults file, *and*
1560Sstevel@tonic-gate * internally by other df_get_*() functions.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate const char *
df_get_string(const char * if_name,boolean_t isv6,uint_t param)1603431Scarlsonj df_get_string(const char *if_name, boolean_t isv6, uint_t param)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate char *value;
1633431Scarlsonj char paramstr[256];
1643431Scarlsonj char name[256];
1650Sstevel@tonic-gate struct stat statbuf;
1660Sstevel@tonic-gate static struct stat df_statbuf;
1670Sstevel@tonic-gate static boolean_t df_unavail_msg = B_FALSE;
1680Sstevel@tonic-gate static nvlist_t *df_nvlist = NULL;
1690Sstevel@tonic-gate
1703431Scarlsonj if (param >= (sizeof (defaults) / sizeof (*defaults)))
1710Sstevel@tonic-gate return (NULL);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
1740Sstevel@tonic-gate if (!df_unavail_msg) {
1750Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot access %s; using "
1760Sstevel@tonic-gate "built-in defaults", DHCP_AGENT_DEFAULTS);
1770Sstevel@tonic-gate df_unavail_msg = B_TRUE;
1780Sstevel@tonic-gate }
1793431Scarlsonj return (defaults[param].df_default);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate * if our cached parameters are stale, rebuild.
1840Sstevel@tonic-gate */
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate if (statbuf.st_mtime != df_statbuf.st_mtime ||
1870Sstevel@tonic-gate statbuf.st_size != df_statbuf.st_size) {
1880Sstevel@tonic-gate df_statbuf = statbuf;
1890Sstevel@tonic-gate if (df_nvlist != NULL)
1900Sstevel@tonic-gate nvlist_free(df_nvlist);
1910Sstevel@tonic-gate df_nvlist = df_build_cache();
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate
1943431Scarlsonj if (isv6) {
1953431Scarlsonj (void) snprintf(name, sizeof (name), ".V6.%s",
1963431Scarlsonj defaults[param].df_name);
1973431Scarlsonj (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
1983431Scarlsonj name);
1993431Scarlsonj } else {
2003431Scarlsonj (void) strlcpy(name, defaults[param].df_name, sizeof (name));
2013431Scarlsonj (void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name,
2023431Scarlsonj name);
2033431Scarlsonj }
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate /*
2063431Scarlsonj * first look for `if_name.[v6.]param', then `[v6.]param'. if neither
2070Sstevel@tonic-gate * has been set, use the built-in default.
2080Sstevel@tonic-gate */
2090Sstevel@tonic-gate
2103431Scarlsonj if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 ||
2113431Scarlsonj nvlist_lookup_string(df_nvlist, name, &value) == 0)
2120Sstevel@tonic-gate return (value);
2130Sstevel@tonic-gate
2143431Scarlsonj return (defaults[param].df_default);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * df_get_int(): gets the integer value of a given user-tunable parameter
2190Sstevel@tonic-gate *
2200Sstevel@tonic-gate * input: const char *: the interface the parameter applies to
2213431Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
2223431Scarlsonj * uint_t: the parameter number to look up
2230Sstevel@tonic-gate * output: int: the parameter's value, or default if not set
2240Sstevel@tonic-gate */
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate int
df_get_int(const char * if_name,boolean_t isv6,uint_t param)2273431Scarlsonj df_get_int(const char *if_name, boolean_t isv6, uint_t param)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate const char *value;
2300Sstevel@tonic-gate int value_int;
2310Sstevel@tonic-gate
2323431Scarlsonj if (param >= (sizeof (defaults) / sizeof (*defaults)))
2330Sstevel@tonic-gate return (0);
2340Sstevel@tonic-gate
2353431Scarlsonj value = df_get_string(if_name, isv6, param);
2360Sstevel@tonic-gate if (value == NULL || !isdigit(*value))
2370Sstevel@tonic-gate goto failure;
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate value_int = atoi(value);
2403431Scarlsonj if (value_int > defaults[param].df_max ||
2413431Scarlsonj value_int < defaults[param].df_min)
2420Sstevel@tonic-gate goto failure;
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate return (value_int);
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate failure:
2470Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
2483431Scarlsonj "%d, defaulting to `%s'", defaults[param].df_name,
2493431Scarlsonj defaults[param].df_min, defaults[param].df_max,
2503431Scarlsonj defaults[param].df_default);
2513431Scarlsonj return (atoi(defaults[param].df_default));
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate * df_get_bool(): gets the boolean value of a given user-tunable parameter
2560Sstevel@tonic-gate *
2570Sstevel@tonic-gate * input: const char *: the interface the parameter applies to
2583431Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
2593431Scarlsonj * uint_t: the parameter number to look up
2600Sstevel@tonic-gate * output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
2610Sstevel@tonic-gate */
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate boolean_t
df_get_bool(const char * if_name,boolean_t isv6,uint_t param)2643431Scarlsonj df_get_bool(const char *if_name, boolean_t isv6, uint_t param)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate const char *value;
2670Sstevel@tonic-gate
2683431Scarlsonj if (param >= (sizeof (defaults) / sizeof (*defaults)))
2690Sstevel@tonic-gate return (0);
2700Sstevel@tonic-gate
2713431Scarlsonj value = df_get_string(if_name, isv6, param);
2720Sstevel@tonic-gate if (value != NULL) {
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (strcasecmp(value, "true") == 0 ||
2750Sstevel@tonic-gate strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
2760Sstevel@tonic-gate return (B_TRUE);
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate if (strcasecmp(value, "false") == 0 ||
2790Sstevel@tonic-gate strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
2800Sstevel@tonic-gate return (B_FALSE);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
2843431Scarlsonj "`%s', defaulting to `%s'", defaults[param].df_name,
2853431Scarlsonj value != NULL ? value : "NULL", defaults[param].df_default);
2860Sstevel@tonic-gate
2873431Scarlsonj return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE);
2880Sstevel@tonic-gate }
289