#include <stdio.h>
#include <net/ethernet.h>
#include <sys/param.h>
+#include <sys/ioctl.h>
#include "util.h"
#include "list.h"
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);
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;
}
uint8_t *opt;
optlen = DHCP_MIN_OPTIONS_SIZE;
- len = DHCP_MESSAGE_SIZE + optlen;
+ len = sizeof(DHCPPacket) + optlen;
request = malloc0(len);
if (!request)
&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->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)
+ 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))
+ 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;
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_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_raw(sd_event_source *s, int fd,
uint32_t revents, void *userdata) {
sd_dhcp_client *client = userdata;
- uint8_t buf[sizeof(DHCPPacket) + DHCP_MIN_OPTIONS_SIZE];
- int buflen = sizeof(buf);
- int len, r = 0;
- DHCPPacket *packet;
+ _cleanup_free_ DHCPPacket *packet = NULL;
usec_t time_now;
+ uint8_t cmsgbuf[CMSG_LEN(sizeof(struct tpacket_auxdata))];
+ struct iovec iov = {};
+ struct msghdr msg = {
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ .msg_control = cmsgbuf,
+ .msg_controllen = sizeof(cmsgbuf),
+ };
+ struct cmsghdr *cmsg;
+ bool checksum = true;
+ int buflen = 0, len, r;
assert(s);
assert(client);
assert(client->event);
- len = read(fd, &buf, buflen);
- if (len < 0)
+ r = ioctl(fd, FIONREAD, &buflen);
+ if (r < 0 || buflen <= 0)
+ buflen = sizeof(DHCPPacket) + DHCP_MIN_OPTIONS_SIZE;
+
+ packet = malloc0(buflen);
+ if (!packet)
+ return -ENOMEM;
+
+ iov.iov_base = packet;
+ iov.iov_len = buflen;
+
+ len = recvmsg(fd, &msg, 0);
+ if (len < 0) {
+ log_dhcp_client(client, "could not receive message from raw "
+ "socket: %s", strerror(errno));
return 0;
+ }
- packet = (DHCPPacket *) buf;
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_level == SOL_PACKET && cmsg->cmsg_type == PACKET_AUXDATA) {
+ struct tpacket_auxdata *aux = (void *)CMSG_DATA(cmsg);
- r = dhcp_packet_verify_headers(packet, BOOTREPLY, len);
+ checksum = !(aux->tp_status & TP_STATUS_CSUMNOTREADY);
+ break;
+ }
+ }
+
+ r = dhcp_packet_verify_headers(packet, len, checksum);
if (r < 0)
return 0;
client->start_time = now(CLOCK_MONOTONIC);
client->secs = 0;
+ log_dhcp_client(client, "STARTED");
+
return client_initialize_events(client, client_receive_message_raw,
client->start_time);
}