#include <stdio.h>
#include <net/ethernet.h>
#include <sys/param.h>
+#include <sys/ioctl.h>
#include "util.h"
#include "list.h"
#include "dhcp-internal.h"
#include "sd-dhcp-client.h"
-#define DHCP_CLIENT_MIN_OPTIONS_SIZE 312
-
struct sd_dhcp_client {
DHCPState state;
sd_event *event;
if (client->lease)
client->lease = sd_dhcp_lease_unref(client->lease);
+ log_dhcp_client(client, "STOPPED");
+
return 0;
}
size_t *optlen) {
int r;
- r = dhcp_message_init(message, BOOTREQUEST, client->xid, type,
- secs, opt, optlen);
+ assert(secs);
+
+ r = dhcp_message_init(message, BOOTREQUEST, client->xid, type, opt,
+ optlen);
if (r < 0)
return r;
+ /* Although 'secs' field is a SHOULD in RFC 2131, certain DHCP servers
+ refuse to issue an DHCP lease if 'secs' is set to zero */
+ message->secs = htobe16(secs);
+
memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
if (client->state == DHCP_STATE_RENEWING ||
client->state == DHCP_STATE_REBINDING)
message->ciaddr = client->lease->address;
- /* Some DHCP servers will refuse to issue an DHCP lease if the Cliient
+ /* Some DHCP servers will refuse to issue an DHCP lease if the Client
Identifier option is not set */
r = dhcp_option_append(opt, optlen, DHCP_OPTION_CLIENT_IDENTIFIER,
ETH_ALEN, &client->mac_addr);
defined default size unless the Maximum Messge Size option
is explicitely set */
max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
- DHCP_CLIENT_MIN_OPTIONS_SIZE);
+ DHCP_MIN_OPTIONS_SIZE);
r = dhcp_option_append(opt, optlen,
DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
2, &max_size);
size_t optlen, len;
uint8_t *opt;
- optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
+ optlen = DHCP_MIN_OPTIONS_SIZE;
len = sizeof(DHCPPacket) + optlen;
discover = malloc0(len);
if (err < 0)
return err;
- dhcp_packet_append_ip_headers(discover, BOOTREQUEST, len);
+ dhcp_packet_append_ip_headers(discover, len);
err = dhcp_network_send_raw_socket(client->fd, &client->link,
discover, len);
+ log_dhcp_client(client, "DISCOVER");
+
return err;
}
int err;
uint8_t *opt;
- optlen = DHCP_CLIENT_MIN_OPTIONS_SIZE;
+ optlen = DHCP_MIN_OPTIONS_SIZE;
len = DHCP_MESSAGE_SIZE + optlen;
request = malloc0(len);
if (client->state == DHCP_STATE_RENEWING) {
err = dhcp_network_send_udp_socket(client->fd,
client->lease->server_address,
+ DHCP_PORT_SERVER,
&request->dhcp,
len - DHCP_IP_UDP_SIZE);
} else {
- dhcp_packet_append_ip_headers(request, BOOTREQUEST, len);
+ dhcp_packet_append_ip_headers(request, len);
err = dhcp_network_send_raw_socket(client->fd, &client->link,
request, len);
}
+ log_dhcp_client(client, "REQUEST");
+
return err;
}
next_timeout += (random_u32() & 0x1fffff);
- r = sd_event_add_monotonic(client->event, next_timeout,
+ client->timeout_resend = sd_event_source_unref(client->timeout_resend);
+
+ r = sd_event_add_monotonic(client->event,
+ &client->timeout_resend,
+ next_timeout,
10 * USEC_PER_MSEC,
- client_timeout_resend, client,
- &client->timeout_resend);
+ client_timeout_resend, client);
if (r < 0)
goto error;
assert(client);
assert(client->event);
- r = sd_event_add_io(client->event, client->fd, EPOLLIN, io_callback,
- client, &client->receive_message);
+ r = sd_event_add_io(client->event, &client->receive_message,
+ client->fd, EPOLLIN, io_callback,
+ client);
if (r < 0)
goto error;
if (r < 0)
goto error;
- r = sd_event_add_monotonic(client->event, usec, 0,
- client_timeout_resend, client,
- &client->timeout_resend);
+ client->timeout_resend = sd_event_source_unref(client->timeout_resend);
+
+ r = sd_event_add_monotonic(client->event,
+ &client->timeout_resend,
+ usec, 0,
+ client_timeout_resend, client);
if (r < 0)
goto error;
void *userdata) {
sd_dhcp_client *client = userdata;
+ log_dhcp_client(client, "EXPIRED");
+
client_stop(client, DHCP_EVENT_EXPIRED);
return 0;
client->fd = r;
+ log_dhcp_client(client, "TIMEOUT T2");
+
return client_initialize_events(client, client_receive_message_raw,
usec);
}
client->attempt = 1;
r = dhcp_network_bind_udp_socket(client->index,
- client->lease->address);
+ client->lease->address,
+ DHCP_PORT_CLIENT);
if (r < 0) {
client_stop(client, r);
return 0;
client->fd = r;
+ log_dhcp_client(client, "TIMEOUT T1");
+
return client_initialize_events(client, client_receive_message_udp, usec);
}
client->lease = lease;
lease = NULL;
+ log_dhcp_client(client, "OFFER");
+
return 0;
}
return r;
r = dhcp_option_parse(ack, len, dhcp_lease_parse_options, lease);
- if (r == DHCP_NAK)
+ if (r == DHCP_NAK) {
+ log_dhcp_client(client, "NAK");
return DHCP_EVENT_NO_LEASE;
+ }
if (r != DHCP_ACK)
return -ENOMSG;
client->lease = lease;
lease = NULL;
+ log_dhcp_client(client, "ACK");
+
return r;
}
if (next_timeout < usec)
return -EINVAL;
- r = sd_event_add_monotonic(client->event, next_timeout,
+ r = sd_event_add_monotonic(client->event,
+ &client->timeout_t1,
+ next_timeout,
10 * USEC_PER_MSEC,
- client_timeout_t1, client,
- &client->timeout_t1);
+ client_timeout_t1, client);
if (r < 0)
return r;
if (next_timeout < usec)
return -EINVAL;
- r = sd_event_add_monotonic(client->event, next_timeout,
+ r = sd_event_add_monotonic(client->event,
+ &client->timeout_t2,
+ next_timeout,
10 * USEC_PER_MSEC,
- client_timeout_t2, client,
- &client->timeout_t2);
+ client_timeout_t2, client);
if (r < 0)
return r;
if (next_timeout < usec)
return -EINVAL;
- r = sd_event_add_monotonic(client->event, next_timeout,
+ r = sd_event_add_monotonic(client->event,
+ &client->timeout_expire, next_timeout,
10 * USEC_PER_MSEC,
- client_timeout_expire, client,
- &client->timeout_expire);
+ client_timeout_expire, client);
if (r < 0)
return r;
assert(client->event);
assert(message);
- if (be32toh(message->xid) != client->xid)
- return -EINVAL;
+ if (len < DHCP_MESSAGE_SIZE) {
+ log_dhcp_client(client, "message too small (%d bytes): "
+ "ignoring", len);
+ 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 (memcmp(&message->chaddr[0], &client->mac_addr.ether_addr_octet,
- ETHER_ADDR_LEN))
- return -EINVAL;
+ ETHER_ADDR_LEN)) {
+ log_dhcp_client(client, "received chaddr does not match "
+ "expected: ignoring");
+ return 0;
+ }
switch (client->state) {
case DHCP_STATE_SELECTING:
client->state = DHCP_STATE_REQUESTING;
client->attempt = 1;
- r = sd_event_add_monotonic(client->event, time_now, 0,
+ r = sd_event_add_monotonic(client->event,
+ &client->timeout_resend,
+ time_now, 0,
client_timeout_resend,
- client,
- &client->timeout_resend);
+ client);
if (r < 0)
goto error;
return 0;
}
-static int client_receive_message_raw(sd_event_source *s, int fd,
+static int client_receive_message_udp(sd_event_source *s, int fd,
uint32_t revents, void *userdata) {
sd_dhcp_client *client = userdata;
- uint8_t buf[sizeof(DHCPMessage) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
- int buflen = sizeof(buf);
- int len, r = 0;
+ _cleanup_free_ DHCPMessage *message = NULL;
+ int buflen = 0, len, r;
usec_t time_now;
assert(s);
assert(client);
assert(client->event);
- len = read(fd, &buf, buflen);
+ r = ioctl(fd, FIONREAD, &buflen);
+ if (r < 0 || buflen <= 0)
+ buflen = sizeof(DHCPMessage) + DHCP_MIN_OPTIONS_SIZE;
+
+ message = malloc0(buflen);
+ if (!message)
+ return -ENOMEM;
+
+ len = read(fd, message, buflen);
if (len < 0)
return 0;
if (r < 0)
return client_stop(client, r);
- return client_handle_message(client, (DHCPMessage *) buf, len,
+ return client_handle_message(client, message, len,
time_now);
}
-static int client_receive_message_udp(sd_event_source *s, int fd,
+static int client_receive_message_raw(sd_event_source *s, int fd,
uint32_t revents, void *userdata) {
sd_dhcp_client *client = userdata;
- uint8_t buf[sizeof(DHCPPacket) + DHCP_CLIENT_MIN_OPTIONS_SIZE];
- int buflen = sizeof(buf);
- int len, r = 0;
- DHCPPacket *packet;
+ _cleanup_free_ DHCPPacket *packet = NULL;
+ int buflen = 0, len, r;
usec_t time_now;
assert(s);
assert(client);
assert(client->event);
- len = read(fd, &buf, buflen);
+ r = ioctl(fd, FIONREAD, &buflen);
+ if (r < 0 || buflen <= 0)
+ buflen = sizeof(DHCPPacket) + DHCP_MIN_OPTIONS_SIZE;
+
+ packet = malloc0(buflen);
+ if (!packet)
+ return -ENOMEM;
+
+ len = read(fd, packet, buflen);
if (len < 0)
return 0;
- packet = (DHCPPacket *) buf;
-
- r = dhcp_packet_verify_headers(packet, BOOTREPLY, len);
+ r = dhcp_packet_verify_headers(packet, len);
if (r < 0)
- return r;
+ return 0;
len -= DHCP_IP_UDP_SIZE;
client->start_time = now(CLOCK_MONOTONIC);
client->secs = 0;
- return client_initialize_events(client, client_receive_message_udp,
+ log_dhcp_client(client, "STARTED");
+
+ return client_initialize_events(client, client_receive_message_raw,
client->start_time);
}