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 /*
22a1196271SJames Carlson * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24*b31320a7SChris Fraire * Copyright (c) 2016-2017, Chris Fraire <cfraire@me.com>.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <ctype.h>
317c478bd9Sstevel@tonic-gate #include <dhcpmsg.h>
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <libnvpair.h>
357c478bd9Sstevel@tonic-gate
36d04ccbb3Scarlsonj #include "common.h"
377c478bd9Sstevel@tonic-gate #include "defaults.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate struct dhcp_default {
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate const char *df_name; /* parameter name */
427c478bd9Sstevel@tonic-gate const char *df_default; /* default value */
437c478bd9Sstevel@tonic-gate int df_min; /* min value if type DF_INTEGER */
447c478bd9Sstevel@tonic-gate int df_max; /* max value if type DF_INTEGER */
457c478bd9Sstevel@tonic-gate };
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate * note: keep in the same order as tunable parameter constants in defaults.h
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate static struct dhcp_default defaults[] = {
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate { "RELEASE_ON_SIGTERM", "0", 0, 0 },
5469bb4bb4Scarlsonj { "IGNORE_FAILED_ARP", "1", 0, -1 },
557c478bd9Sstevel@tonic-gate { "OFFER_WAIT", "3", 1, 20 },
5669bb4bb4Scarlsonj { "ARP_WAIT", "1000", 0, -1 },
577c478bd9Sstevel@tonic-gate { "CLIENT_ID", NULL, 0, 0 },
587c478bd9Sstevel@tonic-gate { "PARAM_REQUEST_LIST", NULL, 0, 0 },
59d04ccbb3Scarlsonj { "REQUEST_HOSTNAME", "1", 0, 0 },
60d04ccbb3Scarlsonj { "DEBUG_LEVEL", "0", 0, 3 },
61a1196271SJames Carlson { "VERBOSE", "0", 0, 0 },
62a1196271SJames Carlson { "VERIFIED_LEASE_ONLY", "0", 0, 0 },
63*b31320a7SChris Fraire { "PARAM_IGNORE_LIST", NULL, 0, 0 },
64*b31320a7SChris Fraire { "REQUEST_FQDN", "1", 0, 0 },
65*b31320a7SChris Fraire { "V4_DEFAULT_IAID_DUID", "0", 0, 0 },
66*b31320a7SChris Fraire { "DNS_DOMAINNAME", NULL, 0, 0 },
67*b31320a7SChris Fraire { "ADOPT_DOMAINNAME", "0", 0, 0 },
687c478bd9Sstevel@tonic-gate };
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate * df_build_cache(): builds the defaults nvlist cache
727c478bd9Sstevel@tonic-gate *
737c478bd9Sstevel@tonic-gate * input: void
747c478bd9Sstevel@tonic-gate * output: a pointer to an nvlist of the current defaults, or NULL on failure
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate static nvlist_t *
df_build_cache(void)787c478bd9Sstevel@tonic-gate df_build_cache(void)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate char entry[1024];
817c478bd9Sstevel@tonic-gate int i;
82d04ccbb3Scarlsonj char *param, *pastv6, *value, *end;
837c478bd9Sstevel@tonic-gate FILE *fp;
847c478bd9Sstevel@tonic-gate nvlist_t *nvlist;
8569bb4bb4Scarlsonj struct dhcp_default *defp;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
887c478bd9Sstevel@tonic-gate return (NULL);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
917c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot build default value cache; "
927c478bd9Sstevel@tonic-gate "using built-in defaults");
937c478bd9Sstevel@tonic-gate (void) fclose(fp);
947c478bd9Sstevel@tonic-gate return (NULL);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate while (fgets(entry, sizeof (entry), fp) != NULL) {
987c478bd9Sstevel@tonic-gate for (i = 0; entry[i] == ' '; i++)
997c478bd9Sstevel@tonic-gate ;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate end = strrchr(entry, '\n');
1027c478bd9Sstevel@tonic-gate value = strchr(entry, '=');
1037c478bd9Sstevel@tonic-gate if (end == NULL || value == NULL || entry[i] == '#')
1047c478bd9Sstevel@tonic-gate continue;
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate *end = '\0';
1077c478bd9Sstevel@tonic-gate *value++ = '\0';
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate * to be compatible with the old defread()-based code
1117c478bd9Sstevel@tonic-gate * which ignored case, store the parameters (except for the
1127c478bd9Sstevel@tonic-gate * leading interface name) in upper case.
1137c478bd9Sstevel@tonic-gate */
1147c478bd9Sstevel@tonic-gate
115d04ccbb3Scarlsonj if ((param = strchr(entry, '.')) == NULL) {
116d04ccbb3Scarlsonj pastv6 = param = entry;
117d04ccbb3Scarlsonj } else {
118d04ccbb3Scarlsonj pastv6 = ++param;
119d04ccbb3Scarlsonj if (strncasecmp(param, "v6.", 3) == 0)
120d04ccbb3Scarlsonj pastv6 += 3;
121d04ccbb3Scarlsonj }
1227c478bd9Sstevel@tonic-gate
12369bb4bb4Scarlsonj for (defp = defaults;
12469bb4bb4Scarlsonj (char *)defp < (char *)defaults + sizeof (defaults);
12569bb4bb4Scarlsonj defp++) {
126d04ccbb3Scarlsonj if (strcasecmp(pastv6, defp->df_name) == 0) {
12769bb4bb4Scarlsonj if (defp->df_max == -1) {
12869bb4bb4Scarlsonj dhcpmsg(MSG_WARNING, "parameter %s is "
12969bb4bb4Scarlsonj "obsolete; ignored", defp->df_name);
13069bb4bb4Scarlsonj }
13169bb4bb4Scarlsonj break;
13269bb4bb4Scarlsonj }
13369bb4bb4Scarlsonj }
13469bb4bb4Scarlsonj
1357c478bd9Sstevel@tonic-gate for (; *param != '\0'; param++)
1367c478bd9Sstevel@tonic-gate *param = toupper(*param);
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
1397c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot build default value cache;"
1407c478bd9Sstevel@tonic-gate " using built-in defaults");
1417c478bd9Sstevel@tonic-gate nvlist_free(nvlist);
1427c478bd9Sstevel@tonic-gate nvlist = NULL;
1437c478bd9Sstevel@tonic-gate break;
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate (void) fclose(fp);
1487c478bd9Sstevel@tonic-gate return (nvlist);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * df_get_string(): gets the string value of a given user-tunable parameter
1537c478bd9Sstevel@tonic-gate *
1547c478bd9Sstevel@tonic-gate * input: const char *: the interface the parameter applies to
155d04ccbb3Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
156d04ccbb3Scarlsonj * uint_t: the parameter number to look up
1577c478bd9Sstevel@tonic-gate * output: const char *: the parameter's value, or default if not set
1587c478bd9Sstevel@tonic-gate * (must be copied by caller to be kept)
1597c478bd9Sstevel@tonic-gate * NOTE: df_get_string() is both used by functions outside this source
1607c478bd9Sstevel@tonic-gate * file to retrieve strings from the defaults file, *and*
1617c478bd9Sstevel@tonic-gate * internally by other df_get_*() functions.
1627c478bd9Sstevel@tonic-gate */
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate const char *
df_get_string(const char * if_name,boolean_t isv6,uint_t param)165d04ccbb3Scarlsonj df_get_string(const char *if_name, boolean_t isv6, uint_t param)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate char *value;
168d04ccbb3Scarlsonj char paramstr[256];
169d04ccbb3Scarlsonj char name[256];
1707c478bd9Sstevel@tonic-gate struct stat statbuf;
1717c478bd9Sstevel@tonic-gate static struct stat df_statbuf;
1727c478bd9Sstevel@tonic-gate static boolean_t df_unavail_msg = B_FALSE;
1737c478bd9Sstevel@tonic-gate static nvlist_t *df_nvlist = NULL;
1747c478bd9Sstevel@tonic-gate
175d04ccbb3Scarlsonj if (param >= (sizeof (defaults) / sizeof (*defaults)))
1767c478bd9Sstevel@tonic-gate return (NULL);
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
1797c478bd9Sstevel@tonic-gate if (!df_unavail_msg) {
1807c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "cannot access %s; using "
1817c478bd9Sstevel@tonic-gate "built-in defaults", DHCP_AGENT_DEFAULTS);
1827c478bd9Sstevel@tonic-gate df_unavail_msg = B_TRUE;
1837c478bd9Sstevel@tonic-gate }
184d04ccbb3Scarlsonj return (defaults[param].df_default);
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * if our cached parameters are stale, rebuild.
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate if (statbuf.st_mtime != df_statbuf.st_mtime ||
1927c478bd9Sstevel@tonic-gate statbuf.st_size != df_statbuf.st_size) {
1937c478bd9Sstevel@tonic-gate df_statbuf = statbuf;
1947c478bd9Sstevel@tonic-gate nvlist_free(df_nvlist);
1957c478bd9Sstevel@tonic-gate df_nvlist = df_build_cache();
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
198d04ccbb3Scarlsonj if (isv6) {
199d04ccbb3Scarlsonj (void) snprintf(name, sizeof (name), ".V6.%s",
200d04ccbb3Scarlsonj defaults[param].df_name);
201d04ccbb3Scarlsonj (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
202d04ccbb3Scarlsonj name);
203d04ccbb3Scarlsonj } else {
204d04ccbb3Scarlsonj (void) strlcpy(name, defaults[param].df_name, sizeof (name));
205d04ccbb3Scarlsonj (void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name,
206d04ccbb3Scarlsonj name);
207d04ccbb3Scarlsonj }
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate /*
210d04ccbb3Scarlsonj * first look for `if_name.[v6.]param', then `[v6.]param'. if neither
2117c478bd9Sstevel@tonic-gate * has been set, use the built-in default.
2127c478bd9Sstevel@tonic-gate */
2137c478bd9Sstevel@tonic-gate
214d04ccbb3Scarlsonj if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 ||
215d04ccbb3Scarlsonj nvlist_lookup_string(df_nvlist, name, &value) == 0)
2167c478bd9Sstevel@tonic-gate return (value);
2177c478bd9Sstevel@tonic-gate
218d04ccbb3Scarlsonj return (defaults[param].df_default);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate * df_get_int(): gets the integer value of a given user-tunable parameter
2237c478bd9Sstevel@tonic-gate *
2247c478bd9Sstevel@tonic-gate * input: const char *: the interface the parameter applies to
225d04ccbb3Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
226d04ccbb3Scarlsonj * uint_t: the parameter number to look up
2277c478bd9Sstevel@tonic-gate * output: int: the parameter's value, or default if not set
2287c478bd9Sstevel@tonic-gate */
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate int
df_get_int(const char * if_name,boolean_t isv6,uint_t param)231d04ccbb3Scarlsonj df_get_int(const char *if_name, boolean_t isv6, uint_t param)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate const char *value;
2347c478bd9Sstevel@tonic-gate int value_int;
2357c478bd9Sstevel@tonic-gate
236d04ccbb3Scarlsonj if (param >= (sizeof (defaults) / sizeof (*defaults)))
2377c478bd9Sstevel@tonic-gate return (0);
2387c478bd9Sstevel@tonic-gate
239d04ccbb3Scarlsonj value = df_get_string(if_name, isv6, param);
2407c478bd9Sstevel@tonic-gate if (value == NULL || !isdigit(*value))
2417c478bd9Sstevel@tonic-gate goto failure;
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate value_int = atoi(value);
244d04ccbb3Scarlsonj if (value_int > defaults[param].df_max ||
245d04ccbb3Scarlsonj value_int < defaults[param].df_min)
2467c478bd9Sstevel@tonic-gate goto failure;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate return (value_int);
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate failure:
2517c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
252d04ccbb3Scarlsonj "%d, defaulting to `%s'", defaults[param].df_name,
253d04ccbb3Scarlsonj defaults[param].df_min, defaults[param].df_max,
254d04ccbb3Scarlsonj defaults[param].df_default);
255d04ccbb3Scarlsonj return (atoi(defaults[param].df_default));
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate * df_get_bool(): gets the boolean value of a given user-tunable parameter
2607c478bd9Sstevel@tonic-gate *
2617c478bd9Sstevel@tonic-gate * input: const char *: the interface the parameter applies to
262d04ccbb3Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
263d04ccbb3Scarlsonj * uint_t: the parameter number to look up
2647c478bd9Sstevel@tonic-gate * output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
2657c478bd9Sstevel@tonic-gate */
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate boolean_t
df_get_bool(const char * if_name,boolean_t isv6,uint_t param)268d04ccbb3Scarlsonj df_get_bool(const char *if_name, boolean_t isv6, uint_t param)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate const char *value;
2717c478bd9Sstevel@tonic-gate
272d04ccbb3Scarlsonj if (param >= (sizeof (defaults) / sizeof (*defaults)))
2737c478bd9Sstevel@tonic-gate return (0);
2747c478bd9Sstevel@tonic-gate
275d04ccbb3Scarlsonj value = df_get_string(if_name, isv6, param);
2767c478bd9Sstevel@tonic-gate if (value != NULL) {
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate if (strcasecmp(value, "true") == 0 ||
2797c478bd9Sstevel@tonic-gate strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
2807c478bd9Sstevel@tonic-gate return (B_TRUE);
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate if (strcasecmp(value, "false") == 0 ||
2837c478bd9Sstevel@tonic-gate strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
2847c478bd9Sstevel@tonic-gate return (B_FALSE);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
288d04ccbb3Scarlsonj "`%s', defaulting to `%s'", defaults[param].df_name,
289d04ccbb3Scarlsonj value != NULL ? value : "NULL", defaults[param].df_default);
2907c478bd9Sstevel@tonic-gate
291d04ccbb3Scarlsonj return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE);
2927c478bd9Sstevel@tonic-gate }
293