]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Write the lease when bound, not when address applied.
authorRoy Marples <roy@marples.name>
Fri, 10 Oct 2014 09:40:57 +0000 (09:40 +0000)
committerRoy Marples <roy@marples.name>
Fri, 10 Oct 2014 09:40:57 +0000 (09:40 +0000)
ARP check again when moving the same IP to a new interface.
More iface -> ifp changes.

dhcp.c
dhcp.h
ipv4.c

diff --git a/dhcp.c b/dhcp.c
index 38692dc094d0b123544c622ed6171ced390c7620..ea7c159a0581bedebeaa900e55d82c63200069b3 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -1043,7 +1043,7 @@ toobig:
        return -1;
 }
 
-ssize_t
+static ssize_t
 write_lease(const struct interface *ifp, const struct dhcp_message *dhcp)
 {
        int fd;
@@ -1088,7 +1088,7 @@ write_lease(const struct interface *ifp, const struct dhcp_message *dhcp)
        return bytes;
 }
 
-struct dhcp_message *
+static struct dhcp_message *
 read_lease(struct interface *ifp)
 {
        int fd;
@@ -1840,29 +1840,31 @@ dhcp_rebind(void *arg)
 void
 dhcp_bind(void *arg)
 {
-       struct interface *iface = arg;
-       struct dhcp_state *state = D_STATE(iface);
-       struct if_options *ifo = iface->options;
+       struct interface *ifp = arg;
+       struct dhcp_state *state = D_STATE(ifp);
+       struct if_options *ifo = ifp->options;
        struct dhcp_lease *lease = &state->lease;
        struct timeval tv;
        uint8_t ipv4ll = 0;
 
+       if (state->state == DHS_BOUND)
+               goto applyaddr;
        state->reason = NULL;
        state->xid = 0;
        free(state->old);
        state->old = state->new;
        state->new = state->offer;
        state->offer = NULL;
-       get_lease(iface->ctx, lease, state->new);
+       get_lease(ifp->ctx, lease, state->new);
        if (ifo->options & DHCPCD_STATIC) {
                syslog(LOG_INFO, "%s: using static address %s/%d",
-                   iface->name, inet_ntoa(lease->addr),
+                   ifp->name, inet_ntoa(lease->addr),
                    inet_ntocidr(lease->net));
                lease->leasetime = ~0U;
                state->reason = "STATIC";
        } else if (state->new->cookie != htonl(MAGIC_COOKIE)) {
                syslog(LOG_INFO, "%s: using IPv4LL address %s",
-                   iface->name, inet_ntoa(lease->addr));
+                   ifp->name, inet_ntoa(lease->addr));
                lease->leasetime = ~0U;
                state->reason = "IPV4LL";
                ipv4ll = 1;
@@ -1871,8 +1873,8 @@ dhcp_bind(void *arg)
                        lease->addr.s_addr = ifo->req_addr.s_addr;
                else
                        lease->addr.s_addr = state->addr.s_addr;
-               syslog(LOG_INFO, "%s: received approval for %s", iface->name,
-                   inet_ntoa(lease->addr));
+               syslog(LOG_INFO, "%s: received approval for %s",
+                   ifp->name, inet_ntoa(lease->addr));
                lease->leasetime = ~0U;
                state->reason = "INFORM";
        } else {
@@ -1885,12 +1887,12 @@ dhcp_bind(void *arg)
                            lease->rebindtime =
                            lease->leasetime;
                        syslog(LOG_INFO, "%s: leased %s for infinity",
-                           iface->name, inet_ntoa(lease->addr));
+                           ifp->name, inet_ntoa(lease->addr));
                } else {
                        if (lease->leasetime < DHCP_MIN_LEASE) {
                                syslog(LOG_WARNING,
                                    "%s: minimum lease is %d seconds",
-                                   iface->name, DHCP_MIN_LEASE);
+                                   ifp->name, DHCP_MIN_LEASE);
                                lease->leasetime = DHCP_MIN_LEASE;
                        }
                        if (lease->rebindtime == 0)
@@ -1902,7 +1904,7 @@ dhcp_bind(void *arg)
                                syslog(LOG_WARNING,
                                    "%s: rebind time greater than lease "
                                    "time, forcing to %"PRIu32" seconds",
-                                   iface->name, lease->rebindtime);
+                                   ifp->name, lease->rebindtime);
                        }
                        if (lease->renewaltime == 0)
                                lease->renewaltime =
@@ -1913,18 +1915,18 @@ dhcp_bind(void *arg)
                                syslog(LOG_WARNING,
                                    "%s: renewal time greater than rebind "
                                    "time, forcing to %"PRIu32" seconds",
-                                   iface->name, lease->renewaltime);
+                                   ifp->name, lease->renewaltime);
                        }
                        syslog(lease->addr.s_addr == state->addr.s_addr ?
                            LOG_DEBUG : LOG_INFO,
-                           "%s: leased %s for %"PRIu32" seconds", iface->name,
+                           "%s: leased %s for %"PRIu32" seconds", ifp->name,
                            inet_ntoa(lease->addr), lease->leasetime);
                }
        }
-       if (iface->ctx->options & DHCPCD_TEST) {
+       if (ifp->ctx->options & DHCPCD_TEST) {
                state->reason = "TEST";
-               script_runreason(iface, state->reason);
-               eloop_exit(iface->ctx->eloop, EXIT_SUCCESS);
+               script_runreason(ifp, state->reason);
+               eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS);
                return;
        }
        if (state->reason == NULL) {
@@ -1942,26 +1944,32 @@ dhcp_bind(void *arg)
        if (lease->leasetime == ~0U)
                lease->renewaltime = lease->rebindtime = lease->leasetime;
        else {
-               eloop_timeout_add_sec(iface->ctx->eloop,
-                   (time_t)lease->renewaltime, dhcp_renew, iface);
-               eloop_timeout_add_sec(iface->ctx->eloop,
-                   (time_t)lease->rebindtime, dhcp_rebind, iface);
-               eloop_timeout_add_sec(iface->ctx->eloop,
-                   (time_t)lease->leasetime, dhcp_expire, iface);
+               eloop_timeout_add_sec(ifp->ctx->eloop,
+                   (time_t)lease->renewaltime, dhcp_renew, ifp);
+               eloop_timeout_add_sec(ifp->ctx->eloop,
+                   (time_t)lease->rebindtime, dhcp_rebind, ifp);
+               eloop_timeout_add_sec(ifp->ctx->eloop,
+                   (time_t)lease->leasetime, dhcp_expire, ifp);
                syslog(LOG_DEBUG,
                    "%s: renew in %"PRIu32" seconds, rebind in %"PRIu32
                    " seconds",
-                   iface->name, lease->renewaltime, lease->rebindtime);
+                   ifp->name, lease->renewaltime, lease->rebindtime);
        }
-       ipv4_applyaddr(iface);
-       if (dhcpcd_daemonise(iface->ctx) == 0) {
+       state->state = DHS_BOUND;
+       if (!state->lease.frominfo &&
+           !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)))
+               if (write_lease(ifp, state->new) == -1)
+                       syslog(LOG_ERR, "%s: write_lease: %m", __func__);
+
+applyaddr:
+       ipv4_applyaddr(ifp);
+       if (dhcpcd_daemonise(ifp->ctx) == 0) {
                if (!ipv4ll)
-                       arp_close(iface);
-               state->state = DHS_BOUND;
+                       arp_close(ifp);
                if (ifo->options & DHCPCD_ARP) {
                        state->claims = 0;
                        if (state->added)
-                               arp_announce(iface);
+                               arp_announce(ifp);
                }
        }
 }
diff --git a/dhcp.h b/dhcp.h
index f147a9cc137bc5e3e3ccf20de8ae0609e45c1595..7141023578e72c008c26d606cb8b05f2f3f5eb3f 100644 (file)
--- a/dhcp.h
+++ b/dhcp.h
@@ -272,9 +272,6 @@ ssize_t make_message(struct dhcp_message **, const struct interface *,
     uint8_t);
 int valid_dhcp_packet(unsigned char *);
 
-ssize_t write_lease(const struct interface *, const struct dhcp_message *);
-struct dhcp_message *read_lease(struct interface *);
-
 void dhcp_handleifa(int, struct interface *,
     const struct in_addr *, const struct in_addr *, const struct in_addr *);
 
diff --git a/ipv4.c b/ipv4.c
index b9bcde018dd8227adc4f74ae612a00e3be675f81..15470d4dcd3015ba2df5b3fa42e2a2ba6257d3fe 100644 (file)
--- a/ipv4.c
+++ b/ipv4.c
@@ -46,6 +46,7 @@
 #include <unistd.h>
 
 #include "config.h"
+#include "arp.h"
 #include "common.h"
 #include "dhcpcd.h"
 #include "dhcp.h"
@@ -762,9 +763,18 @@ ipv4_applyaddr(void *arg)
                                        if (nstate && !nstate->added &&
                                            nstate->addr.s_addr == addr.s_addr)
                                        {
-                                               ipv4_addaddr(ifn,
-                                                   &nstate->lease);
-                                               nstate->added = 1;
+                                               if (ifn->options->options &
+                                                   DHCPCD_ARP)
+                                               {
+                                                       nstate->claims = 0;
+                                                       nstate->probes = 0;
+                                                       nstate->conflicts = 0;
+                                                       arp_probe(ifn);
+                                               } else {
+                                                       ipv4_addaddr(ifn,
+                                                           &nstate->lease);
+                                                       nstate->added = 1;
+                                               }
                                                break;
                                        }
                                }
@@ -857,10 +867,6 @@ ipv4_applyaddr(void *arg)
 
 routes:
        ipv4_buildroutes(ifp->ctx);
-       if (!state->lease.frominfo &&
-           !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)))
-               if (write_lease(ifp, dhcp) == -1)
-                       syslog(LOG_ERR, "%s: write_lease: %m", __func__);
        script_runreason(ifp, state->reason);
 }