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
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(-)
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)
22 top_level_config.requested_lease = 7200;
23 + top_level_config.bootp_broadcast_always = 0;
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 */
35 void parse_client_statement (cfile, ip, config)
37 @@ -820,6 +822,12 @@ void parse_client_statement (cfile, ip, config)
38 parse_lease_id_format(cfile);
41 + case BOOTP_BROADCAST_ALWAYS:
42 + token = next_token(&val, (unsigned*)0, cfile);
43 + config -> bootp_broadcast_always = 1;
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
62 +.I dhcp-client-identifier
74 +.I vendor-class-identifier
78 +.I request-option-list
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
97 + * Defined in stdio.h when _GNU_SOURCE is set, but we don't want to define
98 + * that when building ISC code.
100 +extern int asprintf(char **strp, const char *fmt, ...);
102 TIME default_lease_time = 43200; /* 12 hours... */
103 TIME max_lease_time = 86400; /* 24 hours... */
105 @@ -113,6 +119,10 @@ char *mockup_relay = NULL;
107 char *progname = NULL;
109 +int bootp_broadcast_always = 0;
111 +extern struct option *default_requested_options[];
113 void run_stateless(int exit_mode, u_int16_t port);
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]*"
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>]"
128 #define DHCLIENT_USAGEH "{--version|--help|-h}"
131 @@ -249,6 +263,16 @@ main(int argc, char **argv) {
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;
141 + int timeout_arg = 0;
142 + char *arg_conf = NULL;
143 + int arg_conf_len = 0;
145 /* Initialize client globals. */
146 memset(&default_duid, 0, sizeof(default_duid));
148 @@ -564,6 +588,89 @@ main(int argc, char **argv) {
150 } else if (!strcmp(argv[i], "-v")) {
152 + } else if (!strcmp(argv[i], "-C")) {
153 + if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
154 + usage(use_noarg, argv[i-1]);
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);
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]);
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);
177 + if (dhcp_host_name_arg != NULL) {
178 + log_error("The -H <host-name> and -F <fqdn> arguments are mutually exclusive");
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]);
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);
194 + if (dhcp_fqdn_arg != NULL) {
195 + log_error("Only one -F <fqdn> argument can be specified");
199 + if (dhcp_host_name_arg != NULL) {
200 + log_error("The -F <fqdn> and -H <host-name> arguments are mutually exclusive");
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]);
211 + if ((timeout_arg = atoi(argv[i])) <= 0) {
212 + log_error("timeout option must be > 0 - bad value: %s",argv[i]);
215 + } else if (!strcmp(argv[i], "-V")) {
216 + if ((++i == argc) || (argv[i] == NULL) || (*(argv[i])=='\0')) {
217 + usage(use_noarg, argv[i-1]);
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);
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]);
233 + dhclient_request_options = argv[i];
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. */
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);
246 + if ((arg_conf == 0) || (arg_conf_len <= 0))
247 + log_fatal("Unable to send -C option dhcp-client-identifier");
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);
254 + if ((arg_conf == 0) || (arg_conf_len <= 0))
255 + log_fatal("Unable to send -H option host-name");
257 + char *last_arg_conf = arg_conf;
259 + arg_conf_len = asprintf(&arg_conf, "%s\nsend host-name \"%s\";", last_arg_conf, dhcp_host_name_arg);
261 + if ((arg_conf == 0) || (arg_conf_len <= 0))
262 + log_fatal("Unable to send -H option host-name");
264 + free(last_arg_conf);
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);
272 + if ((arg_conf == 0) || (arg_conf_len <= 0))
273 + log_fatal("Unable to send -F option fqdn.fqdn");
275 + char *last_arg_conf = arg_conf;
277 + arg_conf_len = asprintf(&arg_conf, "%s\nsend fqdn.fqdn \"%s\";", last_arg_conf, dhcp_fqdn_arg);
279 + if ((arg_conf == 0) || (arg_conf_len <= 0))
280 + log_fatal("Unable to send -F option fqdn.fqdn");
282 + free(last_arg_conf);
287 + if (arg_conf == 0) {
288 + arg_conf_len = asprintf(&arg_conf, "timeout %d;", timeout_arg);
290 + if ((arg_conf == 0) || (arg_conf_len <= 0))
291 + log_fatal("Unable to process --timeout timeout argument");
293 + char *last_arg_conf = arg_conf;
295 + arg_conf_len = asprintf(&arg_conf, "%s\ntimeout %d;", last_arg_conf, timeout_arg);
297 + if ((arg_conf == 0) || (arg_conf_len == 0))
298 + log_fatal("Unable to process --timeout timeout argument");
300 + free(last_arg_conf);
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);
308 + if ((arg_conf == 0) || (arg_conf_len <= 0))
309 + log_fatal("Unable to send -V option vendor-class-identifier");
311 + char *last_arg_conf = arg_conf;
313 + arg_conf_len = asprintf(&arg_conf, "%s\nsend vendor-class-identifier \"%s\";", last_arg_conf, dhcp_vendor_class_identifier_arg);
315 + if ((arg_conf == 0) || (arg_conf_len <= 0))
316 + log_fatal("Unable to send -V option vendor-class-identifier");
318 + free(last_arg_conf);
322 + if (dhclient_request_options != NULL) {
323 + if (arg_conf == 0) {
324 + arg_conf_len = asprintf(&arg_conf, "request %s;", dhclient_request_options);
326 + if ((arg_conf == 0) || (arg_conf_len <= 0))
327 + log_fatal("Unable to parse --request-options <request options list> argument");
329 + char *last_arg_conf = arg_conf;
331 + arg_conf_len = asprintf(&arg_conf, "%s\nrequest %s;", last_arg_conf, dhclient_request_options);
333 + if ((arg_conf == 0) || (arg_conf_len <= 0))
334 + log_fatal("Unable to parse --request-options <request options list> argument");
336 + free(last_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");
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;
352 + status = new_parse(&cfile, -1, arg_conf, arg_conf_len, "extra dhclient -C/-H/-F/--timeout/-V/--request-options configuration arguments", 0);
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 */
359 + token = peek_token(&val, (unsigned *)0, cfile);
360 + if (token == END_OF_FILE)
363 + parse_client_statement(cfile, (struct interface_info *)0, &top_level_config);
366 + if (cfile -> warnings_occurred)
367 + log_fatal("Cannot parse -C/-H/-F/--timeout/-V/--request-options configuration arguments !");
371 + /* we just set the toplevel timeout, but per-client
372 + * timeouts may still be at defaults.
374 + for (ip=interfaces; ip; ip = ip->next) {
375 + if (ip->client->config->timeout == 60)
376 + ip->client->config->timeout = timeout_arg;
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;
392 /* Parse the lease database. */
393 read_client_leases();
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. */
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;
404 client -> packet.flags = htons (BOOTP_BROADCAST);
405 @@ -3557,7 +3815,9 @@ void make_request (client, lease)
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;
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;
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;
434 + if (!strcasecmp (atom + 1, "ootp-broadcast-always"))
435 + return BOOTP_BROADCAST_ALWAYS;
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 {
445 int lease_id_format; /* format for IDs in lease file,
446 TOKEN_OCTAL or TOKEN_HEX */
448 + int bootp_broadcast_always; /* If nonzero, always set the BOOTP_BROADCAST
449 + flag in requests */
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 {
462 + BOOTP_BROADCAST_ALWAYS = 680,
466 #define is_identifier(x) ((x) >= FIRST_TOKEN && \