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*3431Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate #include <sys/types.h> 290Sstevel@tonic-gate #include <stdlib.h> 300Sstevel@tonic-gate #include <string.h> 310Sstevel@tonic-gate #include <ctype.h> 320Sstevel@tonic-gate #include <dhcpmsg.h> 330Sstevel@tonic-gate #include <stdio.h> 340Sstevel@tonic-gate #include <sys/stat.h> 350Sstevel@tonic-gate #include <libnvpair.h> 360Sstevel@tonic-gate 37*3431Scarlsonj #include "common.h" 380Sstevel@tonic-gate #include "defaults.h" 390Sstevel@tonic-gate 400Sstevel@tonic-gate struct dhcp_default { 410Sstevel@tonic-gate 420Sstevel@tonic-gate const char *df_name; /* parameter name */ 430Sstevel@tonic-gate const char *df_default; /* default value */ 440Sstevel@tonic-gate int df_min; /* min value if type DF_INTEGER */ 450Sstevel@tonic-gate int df_max; /* max value if type DF_INTEGER */ 460Sstevel@tonic-gate }; 470Sstevel@tonic-gate 480Sstevel@tonic-gate /* 490Sstevel@tonic-gate * note: keep in the same order as tunable parameter constants in defaults.h 500Sstevel@tonic-gate */ 510Sstevel@tonic-gate 520Sstevel@tonic-gate static struct dhcp_default defaults[] = { 530Sstevel@tonic-gate 540Sstevel@tonic-gate { "RELEASE_ON_SIGTERM", "0", 0, 0 }, 552546Scarlsonj { "IGNORE_FAILED_ARP", "1", 0, -1 }, 560Sstevel@tonic-gate { "OFFER_WAIT", "3", 1, 20 }, 572546Scarlsonj { "ARP_WAIT", "1000", 0, -1 }, 580Sstevel@tonic-gate { "CLIENT_ID", NULL, 0, 0 }, 59*3431Scarlsonj { "PARAM_REQUEST_LIST", NULL, 0, 0 }, 60*3431Scarlsonj { "REQUEST_HOSTNAME", "1", 0, 0 }, 61*3431Scarlsonj { "DEBUG_LEVEL", "0", 0, 3 }, 62*3431Scarlsonj { "VERBOSE", "0", 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 * 730Sstevel@tonic-gate df_build_cache(void) 740Sstevel@tonic-gate { 750Sstevel@tonic-gate char entry[1024]; 760Sstevel@tonic-gate int i; 77*3431Scarlsonj 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 110*3431Scarlsonj if ((param = strchr(entry, '.')) == NULL) { 111*3431Scarlsonj pastv6 = param = entry; 112*3431Scarlsonj } else { 113*3431Scarlsonj pastv6 = ++param; 114*3431Scarlsonj if (strncasecmp(param, "v6.", 3) == 0) 115*3431Scarlsonj pastv6 += 3; 116*3431Scarlsonj } 1170Sstevel@tonic-gate 1182546Scarlsonj for (defp = defaults; 1192546Scarlsonj (char *)defp < (char *)defaults + sizeof (defaults); 1202546Scarlsonj defp++) { 121*3431Scarlsonj 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 150*3431Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP 151*3431Scarlsonj * 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 * 160*3431Scarlsonj df_get_string(const char *if_name, boolean_t isv6, uint_t param) 1610Sstevel@tonic-gate { 1620Sstevel@tonic-gate char *value; 163*3431Scarlsonj char paramstr[256]; 164*3431Scarlsonj 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 170*3431Scarlsonj 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 } 179*3431Scarlsonj 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 194*3431Scarlsonj if (isv6) { 195*3431Scarlsonj (void) snprintf(name, sizeof (name), ".V6.%s", 196*3431Scarlsonj defaults[param].df_name); 197*3431Scarlsonj (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name, 198*3431Scarlsonj name); 199*3431Scarlsonj } else { 200*3431Scarlsonj (void) strlcpy(name, defaults[param].df_name, sizeof (name)); 201*3431Scarlsonj (void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name, 202*3431Scarlsonj name); 203*3431Scarlsonj } 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate /* 206*3431Scarlsonj * 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 210*3431Scarlsonj if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 || 211*3431Scarlsonj nvlist_lookup_string(df_nvlist, name, &value) == 0) 2120Sstevel@tonic-gate return (value); 2130Sstevel@tonic-gate 214*3431Scarlsonj 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 221*3431Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP 222*3431Scarlsonj * 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 227*3431Scarlsonj 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 232*3431Scarlsonj if (param >= (sizeof (defaults) / sizeof (*defaults))) 2330Sstevel@tonic-gate return (0); 2340Sstevel@tonic-gate 235*3431Scarlsonj 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); 240*3431Scarlsonj if (value_int > defaults[param].df_max || 241*3431Scarlsonj 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 " 248*3431Scarlsonj "%d, defaulting to `%s'", defaults[param].df_name, 249*3431Scarlsonj defaults[param].df_min, defaults[param].df_max, 250*3431Scarlsonj defaults[param].df_default); 251*3431Scarlsonj 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 258*3431Scarlsonj * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP 259*3431Scarlsonj * 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 264*3431Scarlsonj 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 268*3431Scarlsonj if (param >= (sizeof (defaults) / sizeof (*defaults))) 2690Sstevel@tonic-gate return (0); 2700Sstevel@tonic-gate 271*3431Scarlsonj 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 " 284*3431Scarlsonj "`%s', defaulting to `%s'", defaults[param].df_name, 285*3431Scarlsonj value != NULL ? value : "NULL", defaults[param].df_default); 2860Sstevel@tonic-gate 287*3431Scarlsonj return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE); 2880Sstevel@tonic-gate } 289