X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flibsystemd-network%2Fsd-dhcp-client.c;h=68bce5e45959c7a9ca0878ecffeff76ecf7b4ef3;hb=0af03ba57f43b973463e15949c2830cbd228f19d;hp=1603c41227ea72148cdbfe21ff23f0e4833c824f;hpb=574cc928887851269c5919123dbdf8e1b2713b23;p=elogind.git diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 1603c4122..68bce5e45 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -48,6 +48,7 @@ struct sd_dhcp_client { int fd; union sockaddr_union link; sd_event_source *receive_message; + bool request_broadcast; uint8_t *req_opts; size_t req_opts_allocated; size_t req_opts_size; @@ -56,6 +57,8 @@ struct sd_dhcp_client { uint8_t type; struct ether_addr mac_addr; } _packed_ client_id; + char *hostname; + char *vendor_class_identifier; uint32_t xid; usec_t start_time; uint16_t secs; @@ -94,6 +97,14 @@ int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb, return 0; } +int sd_dhcp_client_set_request_broadcast(sd_dhcp_client *client, int broadcast) { + assert_return(client, -EINVAL); + + client->request_broadcast = !!broadcast; + + return 0; +} + int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) { size_t i; @@ -178,6 +189,44 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client, return 0; } +int sd_dhcp_client_set_hostname(sd_dhcp_client *client, + const char *hostname) { + char *new_hostname = NULL; + + assert_return(client, -EINVAL); + + if (streq_ptr(client->hostname, hostname)) + return 0; + + if (hostname) { + new_hostname = strdup(hostname); + if (!new_hostname) + return -ENOMEM; + } + + free(client->hostname); + client->hostname = new_hostname; + + return 0; +} + +int sd_dhcp_client_set_vendor_class_identifier(sd_dhcp_client *client, + const char *vci) { + char *new_vci = NULL; + + assert_return(client, -EINVAL); + + new_vci = strdup(vci); + if (!new_vci) + return -ENOMEM; + + free(client->vendor_class_identifier); + + client->vendor_class_identifier = new_vci; + + return 0; +} + int sd_dhcp_client_get_lease(sd_dhcp_client *client, sd_dhcp_lease **ret) { assert_return(client, -EINVAL); assert_return(ret, -EINVAL); @@ -227,19 +276,10 @@ static void client_stop(sd_dhcp_client *client, int error) { if (error < 0) log_dhcp_client(client, "STOPPED: %s", strerror(-error)); - else { - switch(error) { - case DHCP_EVENT_STOP: - log_dhcp_client(client, "STOPPED"); - break; - case DHCP_EVENT_NO_LEASE: - log_dhcp_client(client, "STOPPED: No lease"); - break; - default: - log_dhcp_client(client, "STOPPED: Unknown reason"); - break; - } - } + else if (error == DHCP_EVENT_STOP) + log_dhcp_client(client, "STOPPED"); + else + log_dhcp_client(client, "STOPPED: Unknown event"); client_notify(client, error); @@ -282,8 +322,13 @@ static int client_message_init(sd_dhcp_client *client, DHCPPacket **ret, BROADCAST bit in the 'flags' field to 1 in any DHCPDISCOVER or DHCPREQUEST messages that client sends. The BROADCAST bit will provide a hint to the DHCP server and BOOTP relay agent to broadcast - any messages to the client on the client's subnet. */ - packet->dhcp.flags = htobe16(0x8000); + any messages to the client on the client's subnet. + + Note: some interfaces needs this to be enabled, but some networks + needs this to be disabled as broadcasts are filteretd, so this + needs to be configurable */ + if (client->request_broadcast) + packet->dhcp.flags = htobe16(0x8000); /* RFC2132 section 4.1.1: The client MUST include its hardware address in the ’chaddr’ field, if @@ -358,7 +403,7 @@ static int client_send_discover(sd_dhcp_client *client) { /* See RFC2131 section 4.4.1 */ - r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now); + r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now); if (r < 0) return r; assert(time_now >= client->start_time); @@ -386,6 +431,26 @@ static int client_send_discover(sd_dhcp_client *client) { return r; } + /* it is unclear from RFC 2131 if client should send hostname in + DHCPDISCOVER but dhclient does and so we do as well + */ + if (client->hostname) { + r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, + DHCP_OPTION_HOST_NAME, + strlen(client->hostname), client->hostname); + if (r < 0) + return r; + } + + if (client->vendor_class_identifier) { + r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, + DHCP_OPTION_VENDOR_CLASS_IDENTIFIER, + strlen(client->vendor_class_identifier), + client->vendor_class_identifier); + if (r < 0) + return r; + } + r = dhcp_option_append(&discover->dhcp, optlen, &optoffset, 0, DHCP_OPTION_END, 0, NULL); if (r < 0) @@ -477,6 +542,14 @@ static int client_send_request(sd_dhcp_client *client) { return -EINVAL; } + if (client->hostname) { + r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0, + DHCP_OPTION_HOST_NAME, + strlen(client->hostname), client->hostname); + if (r < 0) + return r; + } + r = dhcp_option_append(&request->dhcp, optlen, &optoffset, 0, DHCP_OPTION_END, 0, NULL); if (r < 0) @@ -530,7 +603,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, assert(client); assert(client->event); - r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now); + r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now); if (r < 0) goto error; @@ -592,7 +665,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec, r = sd_event_add_time(client->event, &client->timeout_resend, - CLOCK_MONOTONIC, + clock_boottime_or_monotonic(), next_timeout, 10 * USEC_PER_MSEC, client_timeout_resend, client); if (r < 0) @@ -658,8 +731,8 @@ error: return 0; } -static int client_initialize_events(sd_dhcp_client *client, - sd_event_io_handler_t io_callback) { +static int client_initialize_io_events(sd_dhcp_client *client, + sd_event_io_handler_t io_callback) { int r; assert(client); @@ -676,11 +749,24 @@ static int client_initialize_events(sd_dhcp_client *client, if (r < 0) goto error; +error: + if (r < 0) + client_stop(client, r); + + return 0; +} + +static int client_initialize_time_events(sd_dhcp_client *client) { + int r; + + assert(client); + assert(client->event); + client->timeout_resend = sd_event_source_unref(client->timeout_resend); r = sd_event_add_time(client->event, &client->timeout_resend, - CLOCK_MONOTONIC, + clock_boottime_or_monotonic(), 0, 0, client_timeout_resend, client); if (r < 0) @@ -697,6 +783,14 @@ error: } +static int client_initialize_events(sd_dhcp_client *client, + sd_event_io_handler_t io_callback) { + client_initialize_io_events(client, io_callback); + client_initialize_time_events(client); + + return 0; +} + static int client_start(sd_dhcp_client *client) { int r; @@ -710,7 +804,7 @@ static int client_start(sd_dhcp_client *client) { client->xid = random_u32(); - r = dhcp_network_bind_raw_socket(client->index, &client->link, client->xid); + r = dhcp_network_bind_raw_socket(client->index, &client->link, client->xid, client->client_id.mac_addr); if (r < 0) { client_stop(client, r); return r; @@ -718,7 +812,7 @@ static int client_start(sd_dhcp_client *client) { client->fd = r; if (client->state == DHCP_STATE_INIT) { - client->start_time = now(CLOCK_MONOTONIC); + client->start_time = now(clock_boottime_or_monotonic()); client->secs = 0; } @@ -754,7 +848,7 @@ static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata) client->state = DHCP_STATE_REBINDING; client->attempt = 1; - r = dhcp_network_bind_raw_socket(client->index, &client->link, client->xid); + r = dhcp_network_bind_raw_socket(client->index, &client->link, client->xid, client->client_id.mac_addr); if (r < 0) { client_stop(client, r); return 0; @@ -773,16 +867,7 @@ static int client_timeout_t1(sd_event_source *s, uint64_t usec, client->state = DHCP_STATE_RENEWING; client->attempt = 1; - r = dhcp_network_bind_udp_socket(client->lease->address, - DHCP_PORT_CLIENT); - if (r < 0) { - client_stop(client, r); - return 0; - } - - client->fd = r; - - return client_initialize_events(client, client_receive_message_udp); + return client_initialize_time_events(client); } static int client_handle_offer(sd_dhcp_client *client, DHCPMessage *offer, @@ -843,7 +928,7 @@ static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack, r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease); if (r == DHCP_NAK) { log_dhcp_client(client, "NAK"); - return DHCP_EVENT_NO_LEASE; + return -EADDRNOTAVAIL; } if (r != DHCP_ACK) { @@ -879,7 +964,8 @@ static int client_handle_ack(sd_dhcp_client *client, DHCPMessage *ack, client->lease->subnet_mask != lease->subnet_mask || client->lease->router != lease->router) { r = DHCP_EVENT_IP_CHANGE; - } + } else + r = DHCP_EVENT_RENEW; client->lease = sd_dhcp_lease_unref(client->lease); } @@ -923,7 +1009,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) { if (client->lease->lifetime == 0xffffffff) return 0; - r = sd_event_now(client->event, CLOCK_MONOTONIC, &time_now); + r = sd_event_now(client->event, clock_boottime_or_monotonic(), &time_now); if (r < 0) return r; assert(client->request_sent <= time_now); @@ -974,7 +1060,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) { /* arm lifetime timeout */ r = sd_event_add_time(client->event, &client->timeout_expire, - CLOCK_MONOTONIC, + clock_boottime_or_monotonic(), lifetime_timeout, 10 * USEC_PER_MSEC, client_timeout_expire, client); if (r < 0) @@ -996,7 +1082,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) { /* arm T2 timeout */ r = sd_event_add_time(client->event, &client->timeout_t2, - CLOCK_MONOTONIC, + clock_boottime_or_monotonic(), t2_timeout, 10 * USEC_PER_MSEC, client_timeout_t2, client); @@ -1019,7 +1105,7 @@ static int client_set_lease_timeouts(sd_dhcp_client *client) { /* arm T1 timeout */ r = sd_event_add_time(client->event, &client->timeout_t1, - CLOCK_MONOTONIC, + clock_boottime_or_monotonic(), t1_timeout, 10 * USEC_PER_MSEC, client_timeout_t1, client); if (r < 0) @@ -1046,35 +1132,6 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, assert(client->event); assert(message); - if (be32toh(message->magic) != DHCP_MAGIC_COOKIE) { - log_dhcp_client(client, "not a DHCP message: ignoring"); - return 0; - } - - if (message->op != BOOTREPLY) { - log_dhcp_client(client, "not a BOOTREPLY message: ignoring"); - return 0; - } - - if (be32toh(message->xid) != client->xid) { - log_dhcp_client(client, "received xid (%u) does not match " - "expected (%u): ignoring", - be32toh(message->xid), client->xid); - return 0; - } - - if (message->htype != ARPHRD_ETHER || message->hlen != ETHER_ADDR_LEN) { - log_dhcp_client(client, "not an ethernet packet"); - return 0; - } - - if (memcmp(&message->chaddr[0], &client->client_id.mac_addr, - ETH_ALEN)) { - log_dhcp_client(client, "received chaddr does not match " - "expected: ignoring"); - return 0; - } - switch (client->state) { case DHCP_STATE_SELECTING: @@ -1089,7 +1146,7 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, r = sd_event_add_time(client->event, &client->timeout_resend, - CLOCK_MONOTONIC, + clock_boottime_or_monotonic(), 0, 0, client_timeout_resend, client); if (r < 0) @@ -1111,25 +1168,7 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, case DHCP_STATE_REBINDING: r = client_handle_ack(client, message, len); - if (r == DHCP_EVENT_NO_LEASE) { - - client->timeout_resend = - sd_event_source_unref(client->timeout_resend); - - if (client->state == DHCP_STATE_REBOOTING) { - r = client_initialize(client); - if (r < 0) - goto error; - - r = client_start(client); - if (r < 0) - goto error; - - log_dhcp_client(client, "REBOOTED"); - } - - goto error; - } else if (r >= 0) { + if (r >= 0) { client->timeout_resend = sd_event_source_unref(client->timeout_resend); @@ -1148,15 +1187,39 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, if (r < 0) goto error; + r = dhcp_network_bind_udp_socket(client->lease->address, + DHCP_PORT_CLIENT); + if (r < 0) { + log_dhcp_client(client, "could not bind UDP socket"); + goto error; + } + + client->fd = r; + + client_initialize_io_events(client, client_receive_message_udp); + if (notify_event) { client_notify(client, notify_event); if (client->state == DHCP_STATE_STOPPED) return 0; } - client->receive_message = - sd_event_source_unref(client->receive_message); - client->fd = asynchronous_close(client->fd); + } else if (r == -EADDRNOTAVAIL) { + /* got a NAK, let's restart the client */ + client->timeout_resend = + sd_event_source_unref(client->timeout_resend); + + r = client_initialize(client); + if (r < 0) + goto error; + + r = client_start(client); + if (r < 0) + goto error; + + log_dhcp_client(client, "REBOOTED"); + + return 0; } else if (r == -ENOMSG) /* invalid message, let's ignore it */ return 0; @@ -1175,7 +1238,7 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message, } error: - if (r < 0 || r == DHCP_EVENT_NO_LEASE) + if (r < 0) client_stop(client, r); return r; @@ -1207,8 +1270,39 @@ static int client_receive_message_udp(sd_event_source *s, int fd, log_dhcp_client(client, "could not receive message from UDP " "socket: %m"); return 0; - } else if ((size_t)len < sizeof(DHCPMessage)) + } else if ((size_t)len < sizeof(DHCPMessage)) { + log_dhcp_client(client, "too small to be a DHCP message: ignoring"); + return 0; + } + + if (be32toh(message->magic) != DHCP_MAGIC_COOKIE) { + log_dhcp_client(client, "not a DHCP message: ignoring"); + return 0; + } + + if (message->op != BOOTREPLY) { + log_dhcp_client(client, "not a BOOTREPLY message: ignoring"); return 0; + } + + if (be32toh(message->xid) != client->xid) { + log_dhcp_client(client, "received xid (%u) does not match " + "expected (%u): ignoring", + be32toh(message->xid), client->xid); + return 0; + } + + if (message->htype != ARPHRD_ETHER || message->hlen != ETHER_ADDR_LEN) { + log_dhcp_client(client, "not an ethernet packet"); + return 0; + } + + if (memcmp(&message->chaddr[0], &client->client_id.mac_addr, + ETH_ALEN)) { + log_dhcp_client(client, "received chaddr does not match " + "expected: ignoring"); + return 0; + } return client_handle_message(client, message, len); } @@ -1363,6 +1457,8 @@ sd_dhcp_client *sd_dhcp_client_unref(sd_dhcp_client *client) { sd_dhcp_lease_unref(client->lease); free(client->req_opts); + free(client->hostname); + free(client->vendor_class_identifier); free(client); }