Print this page
7388 Support DHCP Client FQDN. Allow IAID/DUID for all v4.
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c
+++ new/usr/src/cmd/cmd-inet/sbin/dhcpagent/defaults.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
|
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 + * Copyright (c) 2016, Chris Fraire <cfraire@me.com>.
24 25 */
25 26
26 27 #include <sys/types.h>
27 28 #include <stdlib.h>
28 29 #include <string.h>
29 30 #include <ctype.h>
30 31 #include <dhcpmsg.h>
31 32 #include <stdio.h>
32 33 #include <sys/stat.h>
33 34 #include <libnvpair.h>
34 35
35 36 #include "common.h"
36 37 #include "defaults.h"
37 38
38 39 struct dhcp_default {
39 40
40 41 const char *df_name; /* parameter name */
41 42 const char *df_default; /* default value */
42 43 int df_min; /* min value if type DF_INTEGER */
43 44 int df_max; /* max value if type DF_INTEGER */
44 45 };
45 46
46 47 /*
47 48 * note: keep in the same order as tunable parameter constants in defaults.h
48 49 */
49 50
50 51 static struct dhcp_default defaults[] = {
51 52
|
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
52 53 { "RELEASE_ON_SIGTERM", "0", 0, 0 },
53 54 { "IGNORE_FAILED_ARP", "1", 0, -1 },
54 55 { "OFFER_WAIT", "3", 1, 20 },
55 56 { "ARP_WAIT", "1000", 0, -1 },
56 57 { "CLIENT_ID", NULL, 0, 0 },
57 58 { "PARAM_REQUEST_LIST", NULL, 0, 0 },
58 59 { "REQUEST_HOSTNAME", "1", 0, 0 },
59 60 { "DEBUG_LEVEL", "0", 0, 3 },
60 61 { "VERBOSE", "0", 0, 0 },
61 62 { "VERIFIED_LEASE_ONLY", "0", 0, 0 },
62 - { "PARAM_IGNORE_LIST", NULL, 0, 0 }
63 + { "PARAM_IGNORE_LIST", NULL, 0, 0 },
64 + { "REQUEST_FQDN", "1", 0, 0 },
65 + { "V4_DEFAULT_IAID_DUID", "0", 0, 0 },
63 66 };
64 67
65 68 /*
66 69 * df_build_cache(): builds the defaults nvlist cache
67 70 *
68 71 * input: void
69 72 * output: a pointer to an nvlist of the current defaults, or NULL on failure
70 73 */
71 74
72 75 static nvlist_t *
73 76 df_build_cache(void)
74 77 {
75 78 char entry[1024];
76 79 int i;
77 80 char *param, *pastv6, *value, *end;
78 81 FILE *fp;
79 82 nvlist_t *nvlist;
80 83 struct dhcp_default *defp;
81 84
82 85 if ((fp = fopen(DHCP_AGENT_DEFAULTS, "r")) == NULL)
83 86 return (NULL);
84 87
85 88 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
86 89 dhcpmsg(MSG_WARNING, "cannot build default value cache; "
87 90 "using built-in defaults");
88 91 (void) fclose(fp);
89 92 return (NULL);
90 93 }
91 94
92 95 while (fgets(entry, sizeof (entry), fp) != NULL) {
93 96 for (i = 0; entry[i] == ' '; i++)
94 97 ;
95 98
96 99 end = strrchr(entry, '\n');
97 100 value = strchr(entry, '=');
98 101 if (end == NULL || value == NULL || entry[i] == '#')
99 102 continue;
100 103
101 104 *end = '\0';
102 105 *value++ = '\0';
103 106
104 107 /*
105 108 * to be compatible with the old defread()-based code
106 109 * which ignored case, store the parameters (except for the
107 110 * leading interface name) in upper case.
108 111 */
109 112
110 113 if ((param = strchr(entry, '.')) == NULL) {
111 114 pastv6 = param = entry;
112 115 } else {
113 116 pastv6 = ++param;
114 117 if (strncasecmp(param, "v6.", 3) == 0)
115 118 pastv6 += 3;
116 119 }
117 120
118 121 for (defp = defaults;
119 122 (char *)defp < (char *)defaults + sizeof (defaults);
120 123 defp++) {
121 124 if (strcasecmp(pastv6, defp->df_name) == 0) {
122 125 if (defp->df_max == -1) {
123 126 dhcpmsg(MSG_WARNING, "parameter %s is "
124 127 "obsolete; ignored", defp->df_name);
125 128 }
126 129 break;
127 130 }
128 131 }
129 132
130 133 for (; *param != '\0'; param++)
131 134 *param = toupper(*param);
132 135
133 136 if (nvlist_add_string(nvlist, &entry[i], value) != 0) {
134 137 dhcpmsg(MSG_WARNING, "cannot build default value cache;"
135 138 " using built-in defaults");
136 139 nvlist_free(nvlist);
137 140 nvlist = NULL;
138 141 break;
139 142 }
140 143 }
141 144
142 145 (void) fclose(fp);
143 146 return (nvlist);
144 147 }
145 148
146 149 /*
147 150 * df_get_string(): gets the string value of a given user-tunable parameter
148 151 *
149 152 * input: const char *: the interface the parameter applies to
150 153 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
151 154 * uint_t: the parameter number to look up
152 155 * output: const char *: the parameter's value, or default if not set
153 156 * (must be copied by caller to be kept)
154 157 * NOTE: df_get_string() is both used by functions outside this source
155 158 * file to retrieve strings from the defaults file, *and*
156 159 * internally by other df_get_*() functions.
157 160 */
158 161
159 162 const char *
160 163 df_get_string(const char *if_name, boolean_t isv6, uint_t param)
161 164 {
162 165 char *value;
163 166 char paramstr[256];
164 167 char name[256];
165 168 struct stat statbuf;
166 169 static struct stat df_statbuf;
167 170 static boolean_t df_unavail_msg = B_FALSE;
168 171 static nvlist_t *df_nvlist = NULL;
169 172
170 173 if (param >= (sizeof (defaults) / sizeof (*defaults)))
171 174 return (NULL);
172 175
173 176 if (stat(DHCP_AGENT_DEFAULTS, &statbuf) != 0) {
174 177 if (!df_unavail_msg) {
175 178 dhcpmsg(MSG_WARNING, "cannot access %s; using "
176 179 "built-in defaults", DHCP_AGENT_DEFAULTS);
177 180 df_unavail_msg = B_TRUE;
178 181 }
179 182 return (defaults[param].df_default);
180 183 }
181 184
182 185 /*
183 186 * if our cached parameters are stale, rebuild.
184 187 */
185 188
186 189 if (statbuf.st_mtime != df_statbuf.st_mtime ||
187 190 statbuf.st_size != df_statbuf.st_size) {
188 191 df_statbuf = statbuf;
189 192 nvlist_free(df_nvlist);
190 193 df_nvlist = df_build_cache();
191 194 }
192 195
193 196 if (isv6) {
194 197 (void) snprintf(name, sizeof (name), ".V6.%s",
195 198 defaults[param].df_name);
196 199 (void) snprintf(paramstr, sizeof (paramstr), "%s%s", if_name,
197 200 name);
198 201 } else {
199 202 (void) strlcpy(name, defaults[param].df_name, sizeof (name));
200 203 (void) snprintf(paramstr, sizeof (paramstr), "%s.%s", if_name,
201 204 name);
202 205 }
203 206
204 207 /*
205 208 * first look for `if_name.[v6.]param', then `[v6.]param'. if neither
206 209 * has been set, use the built-in default.
207 210 */
208 211
209 212 if (nvlist_lookup_string(df_nvlist, paramstr, &value) == 0 ||
210 213 nvlist_lookup_string(df_nvlist, name, &value) == 0)
211 214 return (value);
212 215
213 216 return (defaults[param].df_default);
214 217 }
215 218
216 219 /*
217 220 * df_get_int(): gets the integer value of a given user-tunable parameter
218 221 *
219 222 * input: const char *: the interface the parameter applies to
220 223 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
221 224 * uint_t: the parameter number to look up
222 225 * output: int: the parameter's value, or default if not set
223 226 */
224 227
225 228 int
226 229 df_get_int(const char *if_name, boolean_t isv6, uint_t param)
227 230 {
228 231 const char *value;
229 232 int value_int;
230 233
231 234 if (param >= (sizeof (defaults) / sizeof (*defaults)))
232 235 return (0);
233 236
234 237 value = df_get_string(if_name, isv6, param);
235 238 if (value == NULL || !isdigit(*value))
236 239 goto failure;
237 240
238 241 value_int = atoi(value);
239 242 if (value_int > defaults[param].df_max ||
240 243 value_int < defaults[param].df_min)
241 244 goto failure;
242 245
243 246 return (value_int);
244 247
245 248 failure:
246 249 dhcpmsg(MSG_WARNING, "df_get_int: parameter `%s' is not between %d and "
247 250 "%d, defaulting to `%s'", defaults[param].df_name,
248 251 defaults[param].df_min, defaults[param].df_max,
249 252 defaults[param].df_default);
250 253 return (atoi(defaults[param].df_default));
251 254 }
252 255
253 256 /*
254 257 * df_get_bool(): gets the boolean value of a given user-tunable parameter
255 258 *
256 259 * input: const char *: the interface the parameter applies to
257 260 * boolean_t: B_TRUE for DHCPv6, B_FALSE for IPv4 DHCP
258 261 * uint_t: the parameter number to look up
259 262 * output: boolean_t: B_TRUE if true, B_FALSE if false, default if not set
260 263 */
261 264
262 265 boolean_t
263 266 df_get_bool(const char *if_name, boolean_t isv6, uint_t param)
264 267 {
265 268 const char *value;
266 269
267 270 if (param >= (sizeof (defaults) / sizeof (*defaults)))
268 271 return (0);
269 272
270 273 value = df_get_string(if_name, isv6, param);
271 274 if (value != NULL) {
272 275
273 276 if (strcasecmp(value, "true") == 0 ||
274 277 strcasecmp(value, "yes") == 0 || strcmp(value, "1") == 0)
275 278 return (B_TRUE);
276 279
277 280 if (strcasecmp(value, "false") == 0 ||
278 281 strcasecmp(value, "no") == 0 || strcmp(value, "0") == 0)
279 282 return (B_FALSE);
280 283 }
281 284
282 285 dhcpmsg(MSG_WARNING, "df_get_bool: parameter `%s' has invalid value "
283 286 "`%s', defaulting to `%s'", defaults[param].df_name,
284 287 value != NULL ? value : "NULL", defaults[param].df_default);
285 288
286 289 return ((atoi(defaults[param].df_default) == 0) ? B_FALSE : B_TRUE);
287 290 }
|
↓ open down ↓ |
215 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX