]>
Commit | Line | Data |
---|---|---|
1 | From af18c830fe55f6be0b89997a36b611d981e3c25d Mon Sep 17 00:00:00 2001 | |
2 | From: Pavel Zhukov <pzhukov@redhat.com> | |
3 | Date: Thu, 21 Feb 2019 10:19:47 +0100 | |
4 | Subject: [PATCH 02/28] additional dhclient options | |
5 | ||
6 | --- | |
7 | client/clparse.c | 10 +- | |
8 | client/dhclient.8 | 27 +++++ | |
9 | client/dhclient.c | 271 +++++++++++++++++++++++++++++++++++++++++++- | |
10 | common/conflex.c | 2 + | |
11 | includes/dhcpd.h | 3 + | |
12 | includes/dhctoken.h | 3 +- | |
13 | 6 files changed, 309 insertions(+), 7 deletions(-) | |
14 | ||
15 | diff --git a/client/clparse.c b/client/clparse.c | |
16 | index 74ca499..bb63825 100644 | |
17 | --- a/client/clparse.c | |
18 | +++ b/client/clparse.c | |
19 | @@ -192,6 +192,7 @@ isc_result_t read_client_conf () | |
20 | /* Requested lease time, used by DHCPv6 (DHCPv4 uses the option cache) | |
21 | */ | |
22 | top_level_config.requested_lease = 7200; | |
23 | + top_level_config.bootp_broadcast_always = 0; | |
24 | ||
25 | group_allocate (&top_level_config.on_receipt, MDL); | |
26 | if (!top_level_config.on_receipt) | |
27 | @@ -397,7 +398,8 @@ void read_client_leases () | |
28 | interface-declaration | | |
29 | LEASE client-lease-statement | | |
30 | ALIAS client-lease-statement | | |
31 | - KEY key-definition */ | |
32 | + KEY key-definition | | |
33 | + BOOTP_BROADCAST_ALWAYS */ | |
34 | ||
35 | void parse_client_statement (cfile, ip, config) | |
36 | struct parse *cfile; | |
37 | @@ -820,6 +822,12 @@ void parse_client_statement (cfile, ip, config) | |
38 | parse_lease_id_format(cfile); | |
39 | break; | |
40 | ||
41 | + case BOOTP_BROADCAST_ALWAYS: | |
42 | + token = next_token(&val, (unsigned*)0, cfile); | |
43 | + config -> bootp_broadcast_always = 1; | |
44 | + parse_semi (cfile); | |
45 | + return; | |
46 | + | |
47 | ||
48 | default: | |
49 | lose = 0; | |
50 | diff --git a/client/dhclient.8 b/client/dhclient.8 | |
51 | index 861ff56..5029dac 100644 | |
52 | --- a/client/dhclient.8 | |
53 | +++ b/client/dhclient.8 | |
54 | @@ -135,6 +135,33 @@ dhclient - Dynamic Host Configuration Protocol Client | |
55 | .B -w | |
56 | ] | |
57 | [ | |
58 | +.B -B | |
59 | +] | |
60 | +[ | |
61 | +.B -C | |
62 | +.I dhcp-client-identifier | |
63 | +] | |
64 | +[ | |
65 | +.B -H | |
66 | +.I host-name | |
67 | +] | |
68 | +[ | |
69 | +.B -F | |
70 | +.I fqdn.fqdn | |
71 | +] | |
72 | +[ | |
73 | +.B -V | |
74 | +.I vendor-class-identifier | |
75 | +] | |
76 | +[ | |
77 | +.B --request-options | |
78 | +.I request-option-list | |
79 | +] | |
80 | +[ | |
81 | +.B --timeout | |
82 | +.I timeout | |
83 | +] | |
84 | +[ | |
85 | .B --dad-wait-time | |
86 | .I seconds | |
87 | ] | |
88 | diff --git a/client/dhclient.c b/client/dhclient.c | |
89 | index 46dc3a7..6c1c09a 100644 | |
90 | --- a/client/dhclient.c | |
91 | +++ b/client/dhclient.c | |
92 | @@ -41,6 +41,12 @@ | |
93 | #include <sys/wait.h> | |
94 | #include <limits.h> | |
95 | ||
96 | +/* | |
97 | + * Defined in stdio.h when _GNU_SOURCE is set, but we don't want to define | |
98 | + * that when building ISC code. | |
99 | + */ | |
100 | +extern int asprintf(char **strp, const char *fmt, ...); | |
101 | + | |
102 | TIME default_lease_time = 43200; /* 12 hours... */ | |
103 | TIME max_lease_time = 86400; /* 24 hours... */ | |
104 | ||
105 | @@ -113,6 +119,10 @@ char *mockup_relay = NULL; | |
106 | ||
107 | char *progname = NULL; | |
108 | ||
109 | +int bootp_broadcast_always = 0; | |
110 | + | |
111 | +extern struct option *default_requested_options[]; | |
112 | + | |
113 | void run_stateless(int exit_mode, u_int16_t port); | |
114 | ||
115 | static isc_result_t write_duid(struct data_string *duid); | |
116 | @@ -189,8 +199,12 @@ static const char use_v6command[] = "Command not used for DHCPv4: %s"; | |
117 | " [-s server-addr] [-cf config-file]\n" \ | |
118 | " [-df duid-file] [-lf lease-file]\n" \ | |
119 | " [-pf pid-file] [--no-pid] [-e VAR=val]\n" \ | |
120 | -" [-sf script-file] [interface]*" | |
121 | - | |
122 | +" [-sf script-file] [interface]*\n" \ | |
123 | +" [-C <dhcp-client-identifier>] [-B]\n" \ | |
124 | +" [-H <host-name> | -F <fqdn.fqdn>] [--timeout <timeout>]\n" \ | |
125 | +" [-V <vendor-class-identifier>]\n" \ | |
126 | +" [--request-options <request option list>]" | |
127 | + | |
128 | #define DHCLIENT_USAGEH "{--version|--help|-h}" | |
129 | ||
130 | static void | |
131 | @@ -249,6 +263,16 @@ main(int argc, char **argv) { | |
132 | #else | |
133 | progname = argv[0]; | |
134 | #endif | |
135 | + char *dhcp_client_identifier_arg = NULL; | |
136 | + char *dhcp_host_name_arg = NULL; | |
137 | + char *dhcp_fqdn_arg = NULL; | |
138 | + char *dhcp_vendor_class_identifier_arg = NULL; | |
139 | + char *dhclient_request_options = NULL; | |
140 | + | |
141 | + int timeout_arg = 0; | |
142 | + char *arg_conf = NULL; | |
143 | + int arg_conf_len = 0; | |
144 | + | |
145 | /* Initialize client globals. */ | |
146 | memset(&default_duid, 0, sizeof(default_duid)); | |
147 | ||
148 | @@ -564,6 +588,89 @@ main(int argc, char **argv) { | |
149 | std_dhcid = 1; | |
150 | } else if (!strcmp(argv[i], "-v")) { | |
151 | quiet = 0; | |
152 | + } else if (!strcmp(argv[i], "-C")) { | |
153 | + if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) { | |
154 | + usage(use_noarg, argv[i-1]); | |
155 | + exit(1); | |
156 | + } | |
157 | + | |
158 | + if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) { | |
159 | + log_error("-C option dhcp-client-identifier string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1); | |
160 | + exit(1); | |
161 | + } | |
162 | + | |
163 | + dhcp_client_identifier_arg = argv[i]; | |
164 | + } else if (!strcmp(argv[i], "-B")) { | |
165 | + bootp_broadcast_always = 1; | |
166 | + } else if (!strcmp(argv[i], "-H")) { | |
167 | + if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) { | |
168 | + usage(use_noarg, argv[i-1]); | |
169 | + exit(1); | |
170 | + } | |
171 | + | |
172 | + if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) { | |
173 | + log_error("-H option host-name string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1); | |
174 | + exit(1); | |
175 | + } | |
176 | + | |
177 | + if (dhcp_host_name_arg != NULL) { | |
178 | + log_error("The -H <host-name> and -F <fqdn> arguments are mutually exclusive"); | |
179 | + exit(1); | |
180 | + } | |
181 | + | |
182 | + dhcp_host_name_arg = argv[i]; | |
183 | + } else if (!strcmp(argv[i], "-F")) { | |
184 | + if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) { | |
185 | + usage(use_noarg, argv[i-1]); | |
186 | + exit(1); | |
187 | + } | |
188 | + | |
189 | + if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) { | |
190 | + log_error("-F option fqdn.fqdn string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1); | |
191 | + exit(1); | |
192 | + } | |
193 | + | |
194 | + if (dhcp_fqdn_arg != NULL) { | |
195 | + log_error("Only one -F <fqdn> argument can be specified"); | |
196 | + exit(1); | |
197 | + } | |
198 | + | |
199 | + if (dhcp_host_name_arg != NULL) { | |
200 | + log_error("The -F <fqdn> and -H <host-name> arguments are mutually exclusive"); | |
201 | + exit(1); | |
202 | + } | |
203 | + | |
204 | + dhcp_fqdn_arg = argv[i]; | |
205 | + } else if (!strcmp(argv[i], "--timeout")) { | |
206 | + if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) { | |
207 | + usage(use_noarg, argv[i-1]); | |
208 | + exit(1); | |
209 | + } | |
210 | + | |
211 | + if ((timeout_arg = atoi(argv[i])) <= 0) { | |
212 | + log_error("timeout option must be > 0 - bad value: %s",argv[i]); | |
213 | + exit(1); | |
214 | + } | |
215 | + } else if (!strcmp(argv[i], "-V")) { | |
216 | + if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) { | |
217 | + usage(use_noarg, argv[i-1]); | |
218 | + exit(1); | |
219 | + } | |
220 | + | |
221 | + if (strlen(argv[i]) >= DHCP_MAX_OPTION_LEN) { | |
222 | + log_error("-V option vendor-class-identifier string \"%s\" is too long - maximum length is: %d", argv[i], DHCP_MAX_OPTION_LEN-1); | |
223 | + exit(1); | |
224 | + } | |
225 | + | |
226 | + dhcp_vendor_class_identifier_arg = argv[i]; | |
227 | + } else if (!strcmp(argv[i], "--request-options")) { | |
228 | + if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) { | |
229 | + usage(use_noarg, argv[i-1]); | |
230 | + exit(1); | |
231 | + } | |
232 | + | |
233 | + dhclient_request_options = argv[i]; | |
234 | + | |
235 | } else if (argv[i][0] == '-') { | |
236 | usage("Unknown command: %s", argv[i]); | |
237 | } else if (interfaces_requested < 0) { | |
238 | @@ -760,6 +867,156 @@ main(int argc, char **argv) { | |
239 | /* Parse the dhclient.conf file. */ | |
240 | read_client_conf(); | |
241 | ||
242 | + /* Parse any extra command line configuration arguments: */ | |
243 | + if ((dhcp_client_identifier_arg != NULL) && (*dhcp_client_identifier_arg != '\0')) { | |
244 | + arg_conf_len = asprintf(&arg_conf, "send dhcp-client-identifier \"%s\";", dhcp_client_identifier_arg); | |
245 | + | |
246 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
247 | + log_fatal("Unable to send -C option dhcp-client-identifier"); | |
248 | + } | |
249 | + | |
250 | + if ((dhcp_host_name_arg != NULL) && (*dhcp_host_name_arg != '\0')) { | |
251 | + if (arg_conf == 0) { | |
252 | + arg_conf_len = asprintf(&arg_conf, "send host-name \"%s\";", dhcp_host_name_arg); | |
253 | + | |
254 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
255 | + log_fatal("Unable to send -H option host-name"); | |
256 | + } else { | |
257 | + char *last_arg_conf = arg_conf; | |
258 | + arg_conf = NULL; | |
259 | + arg_conf_len = asprintf(&arg_conf, "%s\nsend host-name \"%s\";", last_arg_conf, dhcp_host_name_arg); | |
260 | + | |
261 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
262 | + log_fatal("Unable to send -H option host-name"); | |
263 | + | |
264 | + free(last_arg_conf); | |
265 | + } | |
266 | + } | |
267 | + | |
268 | + if ((dhcp_fqdn_arg != NULL) && (*dhcp_fqdn_arg != '\0')) { | |
269 | + if (arg_conf == 0) { | |
270 | + arg_conf_len = asprintf(&arg_conf, "send fqdn.fqdn \"%s\";", dhcp_fqdn_arg); | |
271 | + | |
272 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
273 | + log_fatal("Unable to send -F option fqdn.fqdn"); | |
274 | + } else { | |
275 | + char *last_arg_conf = arg_conf; | |
276 | + arg_conf = NULL; | |
277 | + arg_conf_len = asprintf(&arg_conf, "%s\nsend fqdn.fqdn \"%s\";", last_arg_conf, dhcp_fqdn_arg); | |
278 | + | |
279 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
280 | + log_fatal("Unable to send -F option fqdn.fqdn"); | |
281 | + | |
282 | + free(last_arg_conf); | |
283 | + } | |
284 | + } | |
285 | + | |
286 | + if (timeout_arg) { | |
287 | + if (arg_conf == 0) { | |
288 | + arg_conf_len = asprintf(&arg_conf, "timeout %d;", timeout_arg); | |
289 | + | |
290 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
291 | + log_fatal("Unable to process --timeout timeout argument"); | |
292 | + } else { | |
293 | + char *last_arg_conf = arg_conf; | |
294 | + arg_conf = NULL; | |
295 | + arg_conf_len = asprintf(&arg_conf, "%s\ntimeout %d;", last_arg_conf, timeout_arg); | |
296 | + | |
297 | + if ((arg_conf == 0) || (arg_conf_len == 0)) | |
298 | + log_fatal("Unable to process --timeout timeout argument"); | |
299 | + | |
300 | + free(last_arg_conf); | |
301 | + } | |
302 | + } | |
303 | + | |
304 | + if ((dhcp_vendor_class_identifier_arg != NULL) && (*dhcp_vendor_class_identifier_arg != '\0')) { | |
305 | + if (arg_conf == 0) { | |
306 | + arg_conf_len = asprintf(&arg_conf, "send vendor-class-identifier \"%s\";", dhcp_vendor_class_identifier_arg); | |
307 | + | |
308 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
309 | + log_fatal("Unable to send -V option vendor-class-identifier"); | |
310 | + } else { | |
311 | + char *last_arg_conf = arg_conf; | |
312 | + arg_conf = NULL; | |
313 | + arg_conf_len = asprintf(&arg_conf, "%s\nsend vendor-class-identifier \"%s\";", last_arg_conf, dhcp_vendor_class_identifier_arg); | |
314 | + | |
315 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
316 | + log_fatal("Unable to send -V option vendor-class-identifier"); | |
317 | + | |
318 | + free(last_arg_conf); | |
319 | + } | |
320 | + } | |
321 | + | |
322 | + if (dhclient_request_options != NULL) { | |
323 | + if (arg_conf == 0) { | |
324 | + arg_conf_len = asprintf(&arg_conf, "request %s;", dhclient_request_options); | |
325 | + | |
326 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
327 | + log_fatal("Unable to parse --request-options <request options list> argument"); | |
328 | + } else { | |
329 | + char *last_arg_conf = arg_conf; | |
330 | + arg_conf = NULL; | |
331 | + arg_conf_len = asprintf(&arg_conf, "%s\nrequest %s;", last_arg_conf, dhclient_request_options); | |
332 | + | |
333 | + if ((arg_conf == 0) || (arg_conf_len <= 0)) | |
334 | + log_fatal("Unable to parse --request-options <request options list> argument"); | |
335 | + | |
336 | + free(last_arg_conf); | |
337 | + } | |
338 | + } | |
339 | + | |
340 | + if (arg_conf) { | |
341 | + if (arg_conf_len == 0) | |
342 | + if ((arg_conf_len = strlen(arg_conf)) == 0) | |
343 | + /* huh ? cannot happen ! */ | |
344 | + log_fatal("Unable to process -C/-H/-F/--timeout/-V/--request-options configuration arguments"); | |
345 | + | |
346 | + /* parse the extra dhclient.conf configuration arguments | |
347 | + * into top level config: */ | |
348 | + struct parse *cfile = (struct parse *)0; | |
349 | + const char *val = NULL; | |
350 | + int token; | |
351 | + | |
352 | + status = new_parse(&cfile, -1, arg_conf, arg_conf_len, "extra dhclient -C/-H/-F/--timeout/-V/--request-options configuration arguments", 0); | |
353 | + | |
354 | + if ((status != ISC_R_SUCCESS) || (cfile -> warnings_occurred)) | |
355 | + log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !"); | |
356 | + /* more detailed parse failures will be logged */ | |
357 | + | |
358 | + do { | |
359 | + token = peek_token(&val, (unsigned *)0, cfile); | |
360 | + if (token == END_OF_FILE) | |
361 | + break; | |
362 | + | |
363 | + parse_client_statement(cfile, (struct interface_info *)0, &top_level_config); | |
364 | + } while (1); | |
365 | + | |
366 | + if (cfile -> warnings_occurred) | |
367 | + log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !"); | |
368 | + end_parse(&cfile); | |
369 | + | |
370 | + if (timeout_arg) { | |
371 | + /* we just set the toplevel timeout, but per-client | |
372 | + * timeouts may still be at defaults. | |
373 | + */ | |
374 | + for (ip=interfaces; ip; ip = ip->next) { | |
375 | + if (ip->client->config->timeout == 60) | |
376 | + ip->client->config->timeout = timeout_arg; | |
377 | + } | |
378 | + } | |
379 | + | |
380 | + if ((dhclient_request_options != 0) && (top_level_config.requested_options != default_requested_options)) { | |
381 | + for (ip=interfaces; ip; ip = ip->next) { | |
382 | + if (ip->client->config->requested_options == default_requested_options) | |
383 | + ip->client->config->requested_options = top_level_config.requested_options; | |
384 | + } | |
385 | + } | |
386 | + | |
387 | + free(arg_conf); | |
388 | + arg_conf = NULL; | |
389 | + arg_conf_len = 0; | |
390 | + } | |
391 | + | |
392 | /* Parse the lease database. */ | |
393 | read_client_leases(); | |
394 | ||
395 | @@ -3472,7 +3729,8 @@ void make_discover (client, lease) | |
396 | client -> packet.xid = random (); | |
397 | client -> packet.secs = 0; /* filled in by send_discover. */ | |
398 | ||
399 | - if (can_receive_unicast_unconfigured (client -> interface)) | |
400 | + if ((!(bootp_broadcast_always || client->config->bootp_broadcast_always)) | |
401 | + && can_receive_unicast_unconfigured(client->interface)) | |
402 | client -> packet.flags = 0; | |
403 | else | |
404 | client -> packet.flags = htons (BOOTP_BROADCAST); | |
405 | @@ -3557,7 +3815,9 @@ void make_request (client, lease) | |
406 | } else { | |
407 | memset (&client -> packet.ciaddr, 0, | |
408 | sizeof client -> packet.ciaddr); | |
409 | - if (can_receive_unicast_unconfigured (client -> interface)) | |
410 | + if ((!(bootp_broadcast_always || | |
411 | + client ->config->bootp_broadcast_always)) && | |
412 | + can_receive_unicast_unconfigured (client -> interface)) | |
413 | client -> packet.flags = 0; | |
414 | else | |
415 | client -> packet.flags = htons (BOOTP_BROADCAST); | |
416 | @@ -3620,7 +3880,8 @@ void make_decline (client, lease) | |
417 | client -> packet.hops = 0; | |
418 | client -> packet.xid = client -> xid; | |
419 | client -> packet.secs = 0; /* Filled in by send_request. */ | |
420 | - if (can_receive_unicast_unconfigured (client -> interface)) | |
421 | + if ((!(bootp_broadcast_always || client->config-> bootp_broadcast_always)) | |
422 | + && can_receive_unicast_unconfigured (client->interface)) | |
423 | client -> packet.flags = 0; | |
424 | else | |
425 | client -> packet.flags = htons (BOOTP_BROADCAST); | |
426 | diff --git a/common/conflex.c b/common/conflex.c | |
427 | index 8b01dfb..1fa2be3 100644 | |
428 | --- a/common/conflex.c | |
429 | +++ b/common/conflex.c | |
430 | @@ -832,6 +832,8 @@ intern(char *atom, enum dhcp_token dfv) { | |
431 | if (!strcasecmp(atom+1, "ig-endian")) { | |
432 | return TOKEN_BIG_ENDIAN; | |
433 | } | |
434 | + if (!strcasecmp (atom + 1, "ootp-broadcast-always")) | |
435 | + return BOOTP_BROADCAST_ALWAYS; | |
436 | break; | |
437 | case 'c': | |
438 | if (!strcasecmp(atom + 1, "ase")) | |
439 | diff --git a/includes/dhcpd.h b/includes/dhcpd.h | |
440 | index f68b228..3b2e2ca 100644 | |
441 | --- a/includes/dhcpd.h | |
442 | +++ b/includes/dhcpd.h | |
443 | @@ -1284,6 +1284,9 @@ struct client_config { | |
444 | ||
445 | int lease_id_format; /* format for IDs in lease file, | |
446 | TOKEN_OCTAL or TOKEN_HEX */ | |
447 | + | |
448 | + int bootp_broadcast_always; /* If nonzero, always set the BOOTP_BROADCAST | |
449 | + flag in requests */ | |
450 | }; | |
451 | ||
452 | /* Per-interface state used in the dhcp client... */ | |
453 | diff --git a/includes/dhctoken.h b/includes/dhctoken.h | |
454 | index e6d125f..6daa422 100644 | |
455 | --- a/includes/dhctoken.h | |
456 | +++ b/includes/dhctoken.h | |
457 | @@ -377,7 +377,8 @@ enum dhcp_token { | |
458 | TOKEN_HEX = 677, | |
459 | TOKEN_OCTAL = 678, | |
460 | KEY_ALGORITHM = 679, | |
461 | - DISCONNECT = 680 | |
462 | + BOOTP_BROADCAST_ALWAYS = 680, | |
463 | + DISCONNECT = 681 | |
464 | }; | |
465 | ||
466 | #define is_identifier(x) ((x) >= FIRST_TOKEN && \ | |
467 | -- | |
468 | 2.35.1 | |
469 |