[systemd-commits] 12 commits - src/libsystemd-dhcp
Tom Gundersen
tomegun at kemper.freedesktop.org
Sun Dec 22 05:36:57 PST 2013
src/libsystemd-dhcp/dhcp-client.c | 251 ++++++++++++++++++++++++---------
src/libsystemd-dhcp/dhcp-internal.h | 3
src/libsystemd-dhcp/dhcp-network.c | 51 ++++++
src/libsystemd-dhcp/dhcp-protocol.h | 8 -
src/libsystemd-dhcp/test-dhcp-client.c | 26 ++-
5 files changed, 256 insertions(+), 83 deletions(-)
New commits:
commit 23f30ed31218c6bc01d9a0ecebdace43e67e97ff
Author: Tom Gundersen <teg at jklm.no>
Date: Wed Dec 18 10:52:22 2013 +0100
libsystemd-dhcp: add some asserts
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index a8b9354..68a7328 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -1061,7 +1061,7 @@ int sd_dhcp_client_start(sd_dhcp_client *client)
int r;
assert_return(client, -EINVAL);
- assert_return(client->index >= 0, -EINVAL);
+ assert_return(client->index > 0, -EINVAL);
assert_return(client->state == DHCP_STATE_INIT ||
client->state == DHCP_STATE_INIT_REBOOT, -EBUSY);
diff --git a/src/libsystemd-dhcp/dhcp-network.c b/src/libsystemd-dhcp/dhcp-network.c
index ce27f42..b2de67e 100644
--- a/src/libsystemd-dhcp/dhcp-network.c
+++ b/src/libsystemd-dhcp/dhcp-network.c
@@ -34,6 +34,9 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
{
int s;
+ assert(index > 0);
+ assert(link);
+
s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
htons(ETH_P_IP));
if (s < 0)
@@ -77,6 +80,10 @@ int dhcp_network_bind_udp_socket(int index, be32_t client_address)
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
const void *packet, size_t len)
{
+ assert(link);
+ assert(packet);
+ assert(len);
+
if (sendto(s, packet, len, 0, &link->sa, sizeof(link->ll)) < 0)
return -errno;
commit 3dd714003534ab1588203f854ac4cb1a7a233262
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:20 2013 +0200
libsystemd-dhcp: Handle T2 Rebinding timeout
Reuse existing functionality when adding T2 Rebinding support.
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index 23669c7..a8b9354 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -328,7 +328,8 @@ static int client_packet_init(sd_dhcp_client *client, uint8_t type,
refuse to issue an DHCP lease if 'secs' is set to zero */
message->secs = htobe16(secs);
- if (client->state == DHCP_STATE_RENEWING)
+ if (client->state == DHCP_STATE_RENEWING ||
+ client->state == DHCP_STATE_REBINDING)
message->ciaddr = client->lease->address;
memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
@@ -532,13 +533,22 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
break;
+ case DHCP_STATE_REBINDING:
+
+ time_left = (client->lease->lifetime - client->lease->t2)/2;
+ if (time_left < 60)
+ time_left = 60;
+
+ next_timeout = usec + time_left * USEC_PER_SEC;
+
+ break;
+
case DHCP_STATE_INIT:
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_SELECTING:
case DHCP_STATE_REQUESTING:
case DHCP_STATE_BOUND:
- case DHCP_STATE_REBINDING:
if (client->attempt < 64)
client->attempt *= 2;
@@ -581,6 +591,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
case DHCP_STATE_REQUESTING:
case DHCP_STATE_RENEWING:
+ case DHCP_STATE_REBINDING:
err = client_send_request(client, secs);
if (err < 0 && client->attempt >= 64)
goto error;
@@ -592,7 +603,6 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_BOUND:
- case DHCP_STATE_REBINDING:
break;
}
@@ -641,7 +651,28 @@ static int client_timeout_expire(sd_event_source *s, uint64_t usec,
static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
{
- return 0;
+ sd_dhcp_client *client = userdata;
+ int r;
+
+ if (client->fd >= 0) {
+ client->receive_message =
+ sd_event_source_unref(client->receive_message);
+ close(client->fd);
+ client->fd = -1;
+ }
+
+ client->state = DHCP_STATE_REBINDING;
+ client->attempt = 1;
+
+ r = dhcp_network_bind_raw_socket(client->index, &client->link);
+ if (r < 0) {
+ client_stop(client, r);
+ return 0;
+ }
+
+ client->fd = r;
+
+ return client_initialize_events(client, usec);
}
static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
@@ -972,6 +1003,7 @@ static int client_receive_message(sd_event_source *s, int fd,
case DHCP_STATE_REQUESTING:
case DHCP_STATE_RENEWING:
+ case DHCP_STATE_REBINDING:
r = client_receive_ack(client, buf, len);
@@ -1013,7 +1045,6 @@ static int client_receive_message(sd_event_source *s, int fd,
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_BOUND:
- case DHCP_STATE_REBINDING:
break;
}
commit 6a1cd41ef0e7e6c1822ac4def76619a0fddf498a
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:19 2013 +0200
libsystemd-dhcp: Factor out common code initializing events
Factor out common code from timeout T1 handling and starting of
the DHCP client.
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index 1023538..23669c7 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -607,6 +607,28 @@ error:
return 0;
}
+static int client_initialize_events(sd_dhcp_client *client, usec_t usec)
+{
+ int r;
+
+ r = sd_event_add_io(client->event, client->fd, EPOLLIN,
+ client_receive_message, client,
+ &client->receive_message);
+ if (r < 0)
+ goto error;
+
+ r = sd_event_add_monotonic(client->event, usec, 0,
+ client_timeout_resend, client,
+ &client->timeout_resend);
+
+error:
+ if (r < 0)
+ client_stop(client, r);
+
+ return 0;
+
+}
+
static int client_timeout_expire(sd_event_source *s, uint64_t usec,
void *userdata)
{
@@ -632,28 +654,14 @@ static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
r = dhcp_network_bind_udp_socket(client->index,
client->lease->address);
- if (r < 0)
- goto error;
+ if (r < 0) {
+ client_stop(client, r);
+ return 0;
+ }
client->fd = r;
- r = sd_event_add_io(client->event, client->fd, EPOLLIN,
- client_receive_message, client,
- &client->receive_message);
- if (r < 0)
- goto error;
- r = sd_event_add_monotonic(client->event, usec, 0,
- client_timeout_resend, client,
- &client->timeout_resend);
- if (r < 0)
- goto error;
-
- return 0;
-
-error:
- client_stop(client, r);
-
- return 0;
+ return client_initialize_events(client, usec);
}
static int client_parse_offer(uint8_t code, uint8_t len, const uint8_t *option,
@@ -1019,7 +1027,7 @@ error:
int sd_dhcp_client_start(sd_dhcp_client *client)
{
- int err;
+ int r;
assert_return(client, -EINVAL);
assert_return(client->index >= 0, -EINVAL);
@@ -1028,33 +1036,17 @@ int sd_dhcp_client_start(sd_dhcp_client *client)
client->xid = random_u();
- client->fd = dhcp_network_bind_raw_socket(client->index,
- &client->link);
+ r = dhcp_network_bind_raw_socket(client->index, &client->link);
- if (client->fd < 0) {
- err = client->fd;
- goto error;
+ if (r < 0) {
+ client_stop(client, r);
+ return r;
}
- err = sd_event_add_io(client->event, client->fd, EPOLLIN,
- client_receive_message, client,
- &client->receive_message);
- if (err < 0)
- goto error;
-
+ client->fd = r;
client->start_time = now(CLOCK_MONOTONIC);
- err = sd_event_add_monotonic(client->event, client->start_time, 0,
- client_timeout_resend, client,
- &client->timeout_resend);
- if (err < 0)
- goto error;
-
- return 0;
-error:
- client_stop(client, err);
-
- return err;
+ return client_initialize_events(client, client->start_time);
}
int sd_dhcp_client_stop(sd_dhcp_client *client)
commit aba26854e5d47e36fd78a263b271d727d308f0dd
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:18 2013 +0200
libsystemd-dhcp: Handle T1 Renewing timeout
Expiration of T1 timeout takes the client to the Rebinding state,
where it attempts to renew its lease. Start by opening a DCHP unicast
socket as there now is a proper IP address set.
Compute the resend timer as half of the remaining time down to a
minimum of 60 seconds (RFC2131). Modify DHCP Request sending to send
only UDP DHCP data when unicasting. Also modify DHCP Ack/Nak
receiving such that the client_receive_ack() takes care of using
either the full IP, UDP, DHCP packet or only the DHCP payload
depending whether the client is in Requesting or Renewing state.
Finally always report DHCP_EVENT_IP_ACQUIRE from Requesting state
and only DHCP_EVENT_IP_CHANGE if the IP address was modified when
the lease was renewed.
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index 68a3b1a..1023538 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -22,6 +22,7 @@
#include <string.h>
#include <stdio.h>
#include <net/ethernet.h>
+#include <sys/param.h>
#include "util.h"
#include "list.h"
@@ -77,6 +78,9 @@ static const uint8_t default_req_opts[] = {
DHCP_OPTION_NTP_SERVER,
};
+static int client_receive_message(sd_event_source *s, int fd,
+ uint32_t revents, void *userdata);
+
int sd_dhcp_client_set_callback(sd_dhcp_client *client, sd_dhcp_client_cb_t cb,
void *userdata)
{
@@ -324,6 +328,9 @@ static int client_packet_init(sd_dhcp_client *client, uint8_t type,
refuse to issue an DHCP lease if 'secs' is set to zero */
message->secs = htobe16(secs);
+ if (client->state == DHCP_STATE_RENEWING)
+ message->ciaddr = client->lease->address;
+
memcpy(&message->chaddr, &client->mac_addr, ETH_ALEN);
(*opt)[0] = 0x63;
(*opt)[1] = 0x82;
@@ -490,10 +497,17 @@ static int client_send_request(sd_dhcp_client *client, uint16_t secs)
if (err < 0)
return err;
- client_append_ip_headers(request, len);
+ if (client->state == DHCP_STATE_RENEWING) {
+ err = dhcp_network_send_udp_socket(client->fd,
+ client->lease->server_address,
+ &request->dhcp,
+ len - DHCP_IP_UDP_SIZE);
+ } else {
+ client_append_ip_headers(request, len);
- err = dhcp_network_send_raw_socket(client->fd, &client->link,
- request, len);
+ err = dhcp_network_send_raw_socket(client->fd, &client->link,
+ request, len);
+ }
return err;
}
@@ -502,17 +516,39 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
void *userdata)
{
sd_dhcp_client *client = userdata;
- usec_t next_timeout;
+ usec_t next_timeout = 0;
+ uint32_t time_left;
uint16_t secs;
int err = 0;
- secs = (usec - client->start_time) / USEC_PER_SEC;
+ switch (client->state) {
+ case DHCP_STATE_RENEWING:
+
+ time_left = (client->lease->t2 - client->lease->t1)/2;
+ if (time_left < 60)
+ time_left = 60;
- if (client->attempt < 64)
- client->attempt *= 2;
+ next_timeout = usec + time_left * USEC_PER_SEC;
- next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC +
- (random_u() & 0x1fffff);
+ break;
+
+ case DHCP_STATE_INIT:
+ case DHCP_STATE_INIT_REBOOT:
+ case DHCP_STATE_REBOOTING:
+ case DHCP_STATE_SELECTING:
+ case DHCP_STATE_REQUESTING:
+ case DHCP_STATE_BOUND:
+ case DHCP_STATE_REBINDING:
+
+ if (client->attempt < 64)
+ client->attempt *= 2;
+
+ next_timeout = usec + (client->attempt - 1) * USEC_PER_SEC;
+
+ break;
+ }
+
+ next_timeout += (random_u() & 0x1fffff);
err = sd_event_add_monotonic(client->event, next_timeout,
10 * USEC_PER_MSEC,
@@ -521,6 +557,8 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
if (err < 0)
goto error;
+ secs = (usec - client->start_time) / USEC_PER_SEC;
+
switch (client->state) {
case DHCP_STATE_INIT:
err = client_send_discover(client, secs);
@@ -542,6 +580,7 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
break;
case DHCP_STATE_REQUESTING:
+ case DHCP_STATE_RENEWING:
err = client_send_request(client, secs);
if (err < 0 && client->attempt >= 64)
goto error;
@@ -553,7 +592,6 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_BOUND:
- case DHCP_STATE_RENEWING:
case DHCP_STATE_REBINDING:
break;
@@ -586,6 +624,35 @@ static int client_timeout_t2(sd_event_source *s, uint64_t usec, void *userdata)
static int client_timeout_t1(sd_event_source *s, uint64_t usec, void *userdata)
{
+ sd_dhcp_client *client = userdata;
+ int r;
+
+ client->state = DHCP_STATE_RENEWING;
+ client->attempt = 1;
+
+ r = dhcp_network_bind_udp_socket(client->index,
+ client->lease->address);
+ if (r < 0)
+ goto error;
+
+ client->fd = r;
+ r = sd_event_add_io(client->event, client->fd, EPOLLIN,
+ client_receive_message, client,
+ &client->receive_message);
+ if (r < 0)
+ goto error;
+
+ r = sd_event_add_monotonic(client->event, usec, 0,
+ client_timeout_resend, client,
+ &client->timeout_resend);
+ if (r < 0)
+ goto error;
+
+ return 0;
+
+error:
+ client_stop(client, r);
+
return 0;
}
@@ -717,22 +784,32 @@ error:
return -ENOMSG;
}
-static int client_receive_ack(sd_dhcp_client *client, DHCPPacket *offer,
+static int client_receive_ack(sd_dhcp_client *client, const uint8_t *buf,
size_t len)
{
int r;
+ DHCPPacket *ack;
+ DHCPMessage *dhcp;
DHCPLease *lease;
- r = client_verify_headers(client, offer, len);
- if (r < 0)
- return r;
+ if (client->state == DHCP_STATE_RENEWING) {
+ dhcp = (DHCPMessage *)buf;
+ } else {
+ ack = (DHCPPacket *)buf;
+
+ r = client_verify_headers(client, ack, len);
+ if (r < 0)
+ return r;
+
+ dhcp = &ack->dhcp;
+ len -= DHCP_IP_UDP_SIZE;
+ }
lease = new0(DHCPLease, 1);
if (!lease)
return -ENOMEM;
- len = len - DHCP_IP_UDP_SIZE;
- r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer, lease);
+ r = dhcp_option_parse(dhcp, len, client_parse_offer, lease);
if (r == DHCP_NAK) {
r = DHCP_EVENT_NO_LEASE;
@@ -744,7 +821,7 @@ static int client_receive_ack(sd_dhcp_client *client, DHCPPacket *offer,
goto error;
}
- lease->address = offer->dhcp.yiaddr;
+ lease->address = dhcp->yiaddr;
if (lease->address == INADDR_ANY ||
lease->server_address == INADDR_ANY ||
@@ -789,6 +866,10 @@ static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
if (client->lease->lifetime < 10)
return -EINVAL;
+ client->timeout_t1 = sd_event_source_unref(client->timeout_t1);
+ client->timeout_t2 = sd_event_source_unref(client->timeout_t2);
+ client->timeout_expire = sd_event_source_unref(client->timeout_expire);
+
if (!client->lease->t1)
client->lease->t1 = client->lease->lifetime / 2;
@@ -840,13 +921,13 @@ static int client_set_lease_timeouts(sd_dhcp_client *client, uint64_t usec)
return 0;
}
-static int client_receive_raw_message(sd_event_source *s, int fd,
- uint32_t revents, void *userdata)
+static int client_receive_message(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;
+ int len, r = 0, notify_event = 0;
DHCPPacket *message;
usec_t time_now;
@@ -858,11 +939,11 @@ static int client_receive_raw_message(sd_event_source *s, int fd,
if (r < 0)
goto error;
- message = (DHCPPacket *)&buf;
-
switch (client->state) {
case DHCP_STATE_SELECTING:
+ message = (DHCPPacket *)&buf;
+
if (client_receive_offer(client, message, len) >= 0) {
client->timeout_resend =
@@ -882,8 +963,10 @@ static int client_receive_raw_message(sd_event_source *s, int fd,
break;
case DHCP_STATE_REQUESTING:
+ case DHCP_STATE_RENEWING:
+
+ r = client_receive_ack(client, buf, len);
- r = client_receive_ack(client, message, len);
if (r == DHCP_EVENT_NO_LEASE)
goto error;
@@ -891,16 +974,22 @@ static int client_receive_raw_message(sd_event_source *s, int fd,
client->timeout_resend =
sd_event_source_unref(client->timeout_resend);
+ if (client->state == DHCP_STATE_REQUESTING)
+ notify_event = DHCP_EVENT_IP_ACQUIRE;
+ else if (r != DHCP_EVENT_IP_ACQUIRE)
+ notify_event = r;
+
client->state = DHCP_STATE_BOUND;
client->attempt = 1;
client->last_addr = client->lease->address;
r = client_set_lease_timeouts(client, time_now);
- if (r < 0 )
+ if (r < 0)
goto error;
- client_notify(client, DHCP_EVENT_IP_ACQUIRE);
+ if (notify_event)
+ client_notify(client, notify_event);
client->receive_message =
sd_event_source_unref(client->receive_message);
@@ -916,7 +1005,6 @@ static int client_receive_raw_message(sd_event_source *s, int fd,
case DHCP_STATE_INIT_REBOOT:
case DHCP_STATE_REBOOTING:
case DHCP_STATE_BOUND:
- case DHCP_STATE_RENEWING:
case DHCP_STATE_REBINDING:
break;
@@ -949,7 +1037,7 @@ int sd_dhcp_client_start(sd_dhcp_client *client)
}
err = sd_event_add_io(client->event, client->fd, EPOLLIN,
- client_receive_raw_message, client,
+ client_receive_message, client,
&client->receive_message);
if (err < 0)
goto error;
commit 234fc2dfce0e749f6e88301f2c82a3e54ccaa5fa
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:17 2013 +0200
libsystemd-dhcp: Add functions for sending unicast UDP messages
Create a helper functions setting up an unicast DHCP UDP socket and
sending data. Add function stubs for the test program.
[tomegun: initialize structs when allocating, and drop unneccesary 'err']
diff --git a/src/libsystemd-dhcp/dhcp-internal.h b/src/libsystemd-dhcp/dhcp-internal.h
index 4472d95..43b5b1d 100644
--- a/src/libsystemd-dhcp/dhcp-internal.h
+++ b/src/libsystemd-dhcp/dhcp-internal.h
@@ -29,8 +29,11 @@
#include "dhcp-protocol.h"
int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link);
+int dhcp_network_bind_udp_socket(int index, be32_t client_address);
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
const void *packet, size_t len);
+int dhcp_network_send_udp_socket(int s, be32_t server_address,
+ const void *packet, size_t len);
int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
size_t optlen, const void *optval);
diff --git a/src/libsystemd-dhcp/dhcp-network.c b/src/libsystemd-dhcp/dhcp-network.c
index 7fecf27..ce27f42 100644
--- a/src/libsystemd-dhcp/dhcp-network.c
+++ b/src/libsystemd-dhcp/dhcp-network.c
@@ -53,6 +53,27 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
return s;
}
+int dhcp_network_bind_udp_socket(int index, be32_t client_address)
+{
+ int s;
+ union sockaddr_union src = {
+ .in.sin_family = AF_INET,
+ .in.sin_port = htobe16(DHCP_PORT_CLIENT),
+ .in.sin_addr.s_addr = client_address,
+ };
+
+ s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ if (s < 0)
+ return -errno;
+
+ if (bind(s, &src.sa, sizeof(src.in)) < 0) {
+ close_nointr_nofail(s);
+ return -errno;
+ }
+
+ return s;
+}
+
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
const void *packet, size_t len)
{
@@ -61,3 +82,18 @@ int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
return 0;
}
+
+int dhcp_network_send_udp_socket(int s, be32_t server_address,
+ const void *packet, size_t len)
+{
+ union sockaddr_union dest = {
+ .in.sin_family = AF_INET,
+ .in.sin_port = htobe16(DHCP_PORT_SERVER),
+ .in.sin_addr.s_addr = server_address,
+ };
+
+ if (sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in)) < 0)
+ return -errno;
+
+ return 0;
+}
diff --git a/src/libsystemd-dhcp/test-dhcp-client.c b/src/libsystemd-dhcp/test-dhcp-client.c
index 1ff78c1..617236b 100644
--- a/src/libsystemd-dhcp/test-dhcp-client.c
+++ b/src/libsystemd-dhcp/test-dhcp-client.c
@@ -184,6 +184,17 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
return test_fd[0];
}
+int dhcp_network_bind_udp_socket(int index, be32_t client_address)
+{
+ return 0;
+}
+
+int dhcp_network_send_udp_socket(int s, be32_t server_address,
+ const void *packet, size_t len)
+{
+ return 0;
+}
+
static void test_discover_message(sd_event *e)
{
sd_dhcp_client *client;
commit 2fba7b03b515b842f5a33d9e1ec46bfa7767d393
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:16 2013 +0200
libsystemd-dhcp: Check test result without casting value
diff --git a/src/libsystemd-dhcp/test-dhcp-client.c b/src/libsystemd-dhcp/test-dhcp-client.c
index 7400cc6..1ff78c1 100644
--- a/src/libsystemd-dhcp/test-dhcp-client.c
+++ b/src/libsystemd-dhcp/test-dhcp-client.c
@@ -119,13 +119,7 @@ static void test_checksum(void)
0xff, 0xff, 0xff, 0xff
};
- uint8_t check[2] = {
- 0x78, 0xae
- };
-
- uint16_t *val = (uint16_t *)check;
-
- assert(client_checksum(&buf, 20) == *val);
+ assert(client_checksum(&buf, 20) == be16toh(0x78ae));
}
static int check_options(uint8_t code, uint8_t len, const uint8_t *option,
commit 0c6a3c888abd720b5ab28162d1ba4eadffde5272
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:15 2013 +0200
libsystemd-dhcp: Fix checksum computation for buffer with odd size
Fix off-by-one error and notice that summing may need more than one
round for the result to be in the lower 16 bits.
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index 7dc1546..68a3b1a 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -382,10 +382,13 @@ static uint16_t client_checksum(void *buf, int len)
if (len & 0x01) {
odd = buf;
- sum += odd[len];
+ sum += odd[len - 1];
}
- return ~((sum & 0xffff) + (sum >> 16));
+ while (sum >> 16)
+ sum = (sum & 0xffff) + (sum >> 16);
+
+ return ~sum;
}
static void client_append_ip_headers(DHCPPacket *packet, uint16_t len)
diff --git a/src/libsystemd-dhcp/test-dhcp-client.c b/src/libsystemd-dhcp/test-dhcp-client.c
index d398510..7400cc6 100644
--- a/src/libsystemd-dhcp/test-dhcp-client.c
+++ b/src/libsystemd-dhcp/test-dhcp-client.c
@@ -102,10 +102,13 @@ static uint16_t client_checksum(void *buf, int len)
if (len & 0x01) {
odd = buf;
- sum += odd[len];
+ sum += odd[len - 1];
}
- return ~((sum & 0xffff) + (sum >> 16));
+ while (sum >> 16)
+ sum = (sum & 0xffff) + (sum >> 16);
+
+ return ~sum;
}
static void test_checksum(void)
commit 77e8d29dd2ecfa7f11c31c8d96021d323ab3fe3a
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:14 2013 +0200
libsystemd-dhcp: Fix receiving of other message when expecting Ack
When a DHCP Nak is received, return a DHCP_EVENT_NO_LEASE event. If
some other DHCP message is received or an error happens when parsing
options, return -ENOMSG in order to ignore the packet. There may be
more than one server serving the same subnet, each server will send
its Offer to the client.
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index 18a6e3c..7dc1546 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -731,8 +731,15 @@ static int client_receive_ack(sd_dhcp_client *client, DHCPPacket *offer,
len = len - DHCP_IP_UDP_SIZE;
r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer, lease);
- if (r != DHCP_ACK)
+ if (r == DHCP_NAK) {
+ r = DHCP_EVENT_NO_LEASE;
goto error;
+ }
+
+ if (r != DHCP_ACK) {
+ r = -ENOMSG;
+ goto error;
+ }
lease->address = offer->dhcp.yiaddr;
commit 2ed0375c2db786012842f02b84014a70c4806017
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:13 2013 +0200
libsystemd-dhcp: Unref event source before closing socket
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index 043d574..18a6e3c 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -892,10 +892,10 @@ static int client_receive_raw_message(sd_event_source *s, int fd,
client_notify(client, DHCP_EVENT_IP_ACQUIRE);
- close(client->fd);
- client->fd = -1;
client->receive_message =
sd_event_source_unref(client->receive_message);
+ close(client->fd);
+ client->fd = -1;
}
r = 0;
commit 20d471b933890fb47e8f0be499bfd7b40acbd60c
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:12 2013 +0200
libsystemd-dhcp: Return proper error on OOM
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index fe41067..043d574 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -726,7 +726,7 @@ static int client_receive_ack(sd_dhcp_client *client, DHCPPacket *offer,
lease = new0(DHCPLease, 1);
if (!lease)
- return -ENOBUFS;
+ return -ENOMEM;
len = len - DHCP_IP_UDP_SIZE;
r = dhcp_option_parse(&offer->dhcp, len, client_parse_offer, lease);
commit 1c8035a89747ed3aadc433bdd11b865cd0d02472
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:11 2013 +0200
libsystemd-dhcp: Return proper error if bind fails
This also fixes a minor indentation damage.
[tomegun: use close_noint_nofail() instead and drop 'err' variables]
diff --git a/src/libsystemd-dhcp/dhcp-network.c b/src/libsystemd-dhcp/dhcp-network.c
index 83a3084..7fecf27 100644
--- a/src/libsystemd-dhcp/dhcp-network.c
+++ b/src/libsystemd-dhcp/dhcp-network.c
@@ -31,7 +31,7 @@
#include "dhcp-internal.h"
int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
- {
+{
int s;
s = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
@@ -46,7 +46,7 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
memset(link->ll.sll_addr, 0xff, ETH_ALEN);
if (bind(s, &link->sa, sizeof(link->ll)) < 0) {
- close(s);
+ close_nointr_nofail(s);
return -errno;
}
@@ -56,10 +56,8 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
const void *packet, size_t len)
{
- int err = 0;
-
if (sendto(s, packet, len, 0, &link->sa, sizeof(link->ll)) < 0)
- err = -errno;
+ return -errno;
- return err;
+ return 0;
}
commit 3b349af69aab440a625fedb21377e2bdc461098e
Author: Patrik Flykt <patrik.flykt at linux.intel.com>
Date: Fri Dec 20 17:16:10 2013 +0200
libsystemd-dhcp: Use be32_t for IP addresses in network order.
diff --git a/src/libsystemd-dhcp/dhcp-client.c b/src/libsystemd-dhcp/dhcp-client.c
index c0c348c..fe41067 100644
--- a/src/libsystemd-dhcp/dhcp-client.c
+++ b/src/libsystemd-dhcp/dhcp-client.c
@@ -36,10 +36,10 @@ struct DHCPLease {
uint32_t t1;
uint32_t t2;
uint32_t lifetime;
- uint32_t address;
- uint32_t server_address;
- uint32_t subnet_mask;
- uint32_t router;
+ be32_t address;
+ be32_t server_address;
+ be32_t subnet_mask;
+ be32_t router;
};
typedef struct DHCPLease DHCPLease;
@@ -54,7 +54,7 @@ struct sd_dhcp_client {
sd_event_source *receive_message;
uint8_t *req_opts;
size_t req_opts_size;
- uint32_t last_addr;
+ be32_t last_addr;
struct ether_addr mac_addr;
uint32_t xid;
usec_t start_time;
diff --git a/src/libsystemd-dhcp/dhcp-protocol.h b/src/libsystemd-dhcp/dhcp-protocol.h
index 1e59965..76621f5 100644
--- a/src/libsystemd-dhcp/dhcp-protocol.h
+++ b/src/libsystemd-dhcp/dhcp-protocol.h
@@ -36,10 +36,10 @@ struct DHCPMessage {
be32_t xid;
be16_t secs;
be16_t flags;
- uint32_t ciaddr;
- uint32_t yiaddr;
- uint32_t siaddr;
- uint32_t giaddr;
+ be32_t ciaddr;
+ be32_t yiaddr;
+ be32_t siaddr;
+ be32_t giaddr;
uint8_t chaddr[16];
uint8_t sname[64];
uint8_t file[128];
More information about the systemd-commits
mailing list