From: Susant Sahani Date: Sun, 19 Apr 2020 23:34:36 +0000 (+0200) Subject: sd-network: DHCPv4 - Add user class, extra option and send generic options in request... X-Git-Tag: v246-rc1~324 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=579ca0a2b21ac8d8deab70958d79b74b93ff3361;p=thirdparty%2Fsystemd.git sd-network: DHCPv4 - Add user class, extra option and send generic options in request state --- diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index ee0e63e113c..78d93382e72 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -850,36 +850,12 @@ static int dhcp_client_send_raw( packet, len); } -static int client_send_discover(sd_dhcp_client *client) { - _cleanup_free_ DHCPPacket *discover = NULL; - size_t optoffset, optlen; +static int client_append_common_discover_request_options(sd_dhcp_client *client, DHCPPacket *packet, size_t *optoffset, size_t optlen) { sd_dhcp_option *j; Iterator i; int r; assert(client); - assert(IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_SELECTING)); - - r = client_message_init(client, &discover, DHCP_DISCOVER, - &optlen, &optoffset); - if (r < 0) - return r; - - /* the client may suggest values for the network address - and lease time in the DHCPDISCOVER message. The client may include - the ’requested IP address’ option to suggest that a particular IP - address be assigned, and may include the ’IP address lease time’ - option to suggest the lease time it would like. - */ - /* RFC7844 section 3: - SHOULD NOT contain any other option. */ - if (!client->anonymize && client->last_addr != INADDR_ANY) { - r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, - SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, - 4, &client->last_addr); - if (r < 0) - return r; - } if (client->hostname) { /* According to RFC 4702 "clients that send the Client FQDN option in @@ -890,18 +866,18 @@ static int client_send_discover(sd_dhcp_client *client) { /* it is unclear from RFC 2131 if client should send hostname in DHCPDISCOVER but dhclient does and so we do as well */ - r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, + r = dhcp_option_append(&packet->dhcp, optlen, optoffset, 0, SD_DHCP_OPTION_HOST_NAME, strlen(client->hostname), client->hostname); } else - r = client_append_fqdn_option(&discover->dhcp, optlen, &optoffset, + r = client_append_fqdn_option(&packet->dhcp, optlen, optoffset, client->hostname); if (r < 0) return r; } if (client->vendor_class_identifier) { - r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, + r = dhcp_option_append(&packet->dhcp, optlen, optoffset, 0, SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, strlen(client->vendor_class_identifier), client->vendor_class_identifier); @@ -910,7 +886,7 @@ static int client_send_discover(sd_dhcp_client *client) { } if (client->mudurl) { - r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, + r = dhcp_option_append(&packet->dhcp, optlen, optoffset, 0, SD_DHCP_OPTION_MUD_URL, strlen(client->mudurl), client->mudurl); @@ -919,7 +895,7 @@ static int client_send_discover(sd_dhcp_client *client) { } if (client->user_class) { - r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, + r = dhcp_option_append(&packet->dhcp, optlen, optoffset, 0, SD_DHCP_OPTION_USER_CLASS, strv_length(client->user_class), client->user_class); @@ -928,7 +904,7 @@ static int client_send_discover(sd_dhcp_client *client) { } ORDERED_HASHMAP_FOREACH(j, client->extra_options, i) { - r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, + r = dhcp_option_append(&packet->dhcp, optlen, optoffset, 0, j->option, j->length, j->data); if (r < 0) return r; @@ -936,13 +912,50 @@ static int client_send_discover(sd_dhcp_client *client) { if (!ordered_hashmap_isempty(client->vendor_options)) { r = dhcp_option_append( - &discover->dhcp, optlen, &optoffset, 0, + &packet->dhcp, optlen, optoffset, 0, SD_DHCP_OPTION_VENDOR_SPECIFIC, ordered_hashmap_size(client->vendor_options), client->vendor_options); if (r < 0) return r; } + + return 0; +} + +static int client_send_discover(sd_dhcp_client *client) { + _cleanup_free_ DHCPPacket *discover = NULL; + size_t optoffset, optlen; + int r; + + assert(client); + assert(IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_SELECTING)); + + r = client_message_init(client, &discover, DHCP_DISCOVER, + &optlen, &optoffset); + if (r < 0) + return r; + + /* the client may suggest values for the network address + and lease time in the DHCPDISCOVER message. The client may include + the ’requested IP address’ option to suggest that a particular IP + address be assigned, and may include the ’IP address lease time’ + option to suggest the lease time it would like. + */ + /* RFC7844 section 3: + SHOULD NOT contain any other option. */ + if (!client->anonymize && client->last_addr != INADDR_ANY) { + r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, + SD_DHCP_OPTION_REQUESTED_IP_ADDRESS, + 4, &client->last_addr); + if (r < 0) + return r; + } + + r = client_append_common_discover_request_options(client, discover, &optoffset, optlen); + if (r < 0) + return r; + r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, SD_DHCP_OPTION_END, 0, NULL); if (r < 0) @@ -1034,36 +1047,9 @@ static int client_send_request(sd_dhcp_client *client) { return -EINVAL; } - if (client->hostname) { - if (dns_name_is_single_label(client->hostname)) - r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0, - SD_DHCP_OPTION_HOST_NAME, - strlen(client->hostname), client->hostname); - else - r = client_append_fqdn_option(&request->dhcp, optlen, &optoffset, - client->hostname); - if (r < 0) - return r; - } - - if (client->vendor_class_identifier) { - r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0, - SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, - strlen(client->vendor_class_identifier), - client->vendor_class_identifier); - if (r < 0) - return r; - } - - if (client->mudurl) { - r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0, - SD_DHCP_OPTION_MUD_URL, - strlen(client->mudurl), - client->mudurl); - if (r < 0) - return r; - } - + r = client_append_common_discover_request_options(client, request, &optoffset, optlen); + if (r < 0) + return r; r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0, SD_DHCP_OPTION_END, 0, NULL);