chiark / gitweb /
sd-dhcp-client: assert that we can only create DISCOVER or REQUEST messages
[elogind.git] / src / libsystemd-network / sd-dhcp-client.c
index 512bc9368df56e55f029a9dc1f54d766655c66f3..e690f6785ff3fabcd6390ab1ab1625d035179d34 100644 (file)
@@ -96,7 +96,8 @@ int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
         size_t i;
 
         assert_return(client, -EINVAL);
-        assert_return (client->state == DHCP_STATE_INIT, -EBUSY);
+        assert_return (IN_SET(client->state, DHCP_STATE_INIT,
+                              DHCP_STATE_STOPPED), -EBUSY);
 
         switch(option) {
         case DHCP_OPTION_PAD:
@@ -126,7 +127,8 @@ int sd_dhcp_client_set_request_option(sd_dhcp_client *client, uint8_t option) {
 int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
                                        const struct in_addr *last_addr) {
         assert_return(client, -EINVAL);
-        assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
+        assert_return (IN_SET(client->state, DHCP_STATE_INIT,
+                              DHCP_STATE_STOPPED), -EBUSY);
 
         if (last_addr)
                 client->last_addr = last_addr->s_addr;
@@ -138,7 +140,8 @@ int sd_dhcp_client_set_request_address(sd_dhcp_client *client,
 
 int sd_dhcp_client_set_index(sd_dhcp_client *client, int interface_index) {
         assert_return(client, -EINVAL);
-        assert_return(client->state == DHCP_STATE_INIT, -EBUSY);
+        assert_return (IN_SET(client->state, DHCP_STATE_INIT,
+                              DHCP_STATE_STOPPED), -EBUSY);
         assert_return(interface_index >= -1, -EINVAL);
 
         client->index = interface_index;
@@ -156,7 +159,7 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client,
         if (memcmp(&client->client_id.mac_addr, addr, ETH_ALEN) == 0)
                 return 0;
 
-        if (client->state != DHCP_STATE_INIT) {
+        if (!IN_SET(client->state, DHCP_STATE_INIT, DHCP_STATE_STOPPED)) {
                 log_dhcp_client(client, "Changing MAC address on running DHCP "
                                 "client, restarting");
                 need_restart = true;
@@ -169,7 +172,7 @@ int sd_dhcp_client_set_mac(sd_dhcp_client *client,
         memcpy(&client->client_id.mac_addr, addr, ETH_ALEN);
         client->client_id.type = 0x01;
 
-        if (need_restart)
+        if (need_restart && client->state != DHCP_STATE_STOPPED)
                 sd_dhcp_client_start(client);
 
         return 0;
@@ -239,6 +242,7 @@ static sd_dhcp_client *client_stop(sd_dhcp_client *client, int error) {
 
 static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
                                uint8_t type, uint8_t **opt, size_t *optlen) {
+        be16_t max_size;
         int r;
 
         assert(client);
@@ -246,6 +250,7 @@ static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
         assert(message);
         assert(opt);
         assert(optlen);
+        assert(type == DHCP_DISCOVER || type == DHCP_REQUEST);
 
         r = dhcp_message_init(message, BOOTREQUEST, client->xid, type, opt,
                               optlen);
@@ -269,27 +274,23 @@ static int client_message_init(sd_dhcp_client *client, DHCPMessage *message,
         if (r < 0)
                 return r;
 
-        if (type == DHCP_DISCOVER || type == DHCP_REQUEST) {
-                be16_t max_size;
-
-                r = dhcp_option_append(opt, optlen,
-                                       DHCP_OPTION_PARAMETER_REQUEST_LIST,
-                                       client->req_opts_size,
-                                       client->req_opts);
-                if (r < 0)
-                        return r;
+        r = dhcp_option_append(opt, optlen,
+                               DHCP_OPTION_PARAMETER_REQUEST_LIST,
+                               client->req_opts_size,
+                               client->req_opts);
+        if (r < 0)
+                return r;
 
-                /* Some DHCP servers will send bigger DHCP packets than the
-                   defined default size unless the Maximum Messge Size option
-                   is explicitely set */
-                max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
-                                   DHCP_MIN_OPTIONS_SIZE);
-                r = dhcp_option_append(opt, optlen,
-                                       DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
-                                       2, &max_size);
-                if (r < 0)
-                        return r;
-        }
+        /* Some DHCP servers will send bigger DHCP packets than the
+           defined default size unless the Maximum Messge Size option
+           is explicitely set */
+        max_size = htobe16(DHCP_IP_UDP_SIZE + DHCP_MESSAGE_SIZE +
+                           DHCP_MIN_OPTIONS_SIZE);
+        r = dhcp_option_append(opt, optlen,
+                               DHCP_OPTION_MAXIMUM_MESSAGE_SIZE,
+                               2, &max_size);
+        if (r < 0)
+                return r;
 
         return 0;
 }
@@ -404,6 +405,9 @@ static int client_send_request(sd_dhcp_client *client) {
         case DHCP_STATE_REBINDING:
 
                 break;
+
+        case DHCP_STATE_STOPPED:
+                return -EINVAL;
         }
 
         r = dhcp_option_append(&opt, &optlen, DHCP_OPTION_END, 0, NULL);
@@ -467,7 +471,13 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                 /* start over as we did not receive a timely ack or nak */
                 client->state = DHCP_STATE_INIT;
                 client->attempt = 1;
+
+                client->fd = safe_close(client->fd);
                 client->xid = random_u32();
+                r = dhcp_network_bind_raw_socket(client->index, &client->link, client->xid);
+                if (r < 0)
+                        goto error;
+                client->fd = r;
 
                 /* fall through */
         case DHCP_STATE_INIT:
@@ -482,6 +492,10 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
                 next_timeout = time_now + (client->attempt - 1) * USEC_PER_SEC;
 
                 break;
+
+        case DHCP_STATE_STOPPED:
+                r = -EINVAL;
+                goto error;
         }
 
         next_timeout += (random_u32() & 0x1fffff);
@@ -540,6 +554,10 @@ static int client_timeout_resend(sd_event_source *s, uint64_t usec,
         case DHCP_STATE_BOUND:
 
                 break;
+
+        case DHCP_STATE_STOPPED:
+                r = -EINVAL;
+                goto error;
         }
 
         return 0;
@@ -630,8 +648,8 @@ static int client_timeout_expire(sd_event_source *s, uint64_t usec,
 
         client = client_notify(client, DHCP_EVENT_EXPIRED);
 
-        /* lease was lost, start over if not freed */
-        if (client) {
+        /* lease was lost, start over if not freed or stopped in callback */
+        if (client && client->state != DHCP_STATE_STOPPED) {
                 client_initialize(client);
                 client_start(client);
         }
@@ -1046,7 +1064,8 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
 
                         if (notify_event) {
                                 client = client_notify(client, notify_event);
-                                if (!client)
+                                if (!client ||
+                                    client->state == DHCP_STATE_STOPPED)
                                         return 0;
                         }
 
@@ -1064,6 +1083,10 @@ static int client_handle_message(sd_dhcp_client *client, DHCPMessage *message,
         case DHCP_STATE_BOUND:
 
                 break;
+
+        case DHCP_STATE_STOPPED:
+                r = -EINVAL;
+                goto error;
         }
 
 error:
@@ -1177,7 +1200,8 @@ int sd_dhcp_client_start(sd_dhcp_client *client) {
 int sd_dhcp_client_stop(sd_dhcp_client *client) {
         assert_return(client, -EINVAL);
 
-        client_stop(client, DHCP_EVENT_STOP);
+        if (client_stop(client, DHCP_EVENT_STOP))
+                client->state = DHCP_STATE_STOPPED;
 
         return 0;
 }