Home | History | Annotate | Line # | Download | only in rump_dhcpclient
configure.c revision 1.2
      1  1.1   pooka /*
      2  1.1   pooka  * dhcpcd - DHCP client daemon
      3  1.1   pooka  * Copyright (c) 2006-2010 Roy Marples <roy (at) marples.name>
      4  1.1   pooka  * All rights reserved
      5  1.1   pooka 
      6  1.1   pooka  * Redistribution and use in source and binary forms, with or without
      7  1.1   pooka  * modification, are permitted provided that the following conditions
      8  1.1   pooka  * are met:
      9  1.1   pooka  * 1. Redistributions of source code must retain the above copyright
     10  1.1   pooka  *    notice, this list of conditions and the following disclaimer.
     11  1.1   pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1   pooka  *    notice, this list of conditions and the following disclaimer in the
     13  1.1   pooka  *    documentation and/or other materials provided with the distribution.
     14  1.1   pooka  *
     15  1.1   pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  1.1   pooka  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  1.1   pooka  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  1.1   pooka  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.1   pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.1   pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  1.1   pooka  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1   pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.1   pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1   pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1   pooka  * SUCH DAMAGE.
     26  1.1   pooka  */
     27  1.1   pooka 
     28  1.1   pooka #include <sys/stat.h>
     29  1.1   pooka #include <sys/uio.h>
     30  1.1   pooka #include <sys/wait.h>
     31  1.1   pooka 
     32  1.1   pooka #include <netinet/in.h>
     33  1.1   pooka #include <arpa/inet.h>
     34  1.1   pooka 
     35  1.1   pooka #include <ctype.h>
     36  1.1   pooka #include <errno.h>
     37  1.1   pooka #include <signal.h>
     38  1.1   pooka #include <stdlib.h>
     39  1.1   pooka #include <string.h>
     40  1.1   pooka #include <unistd.h>
     41  1.1   pooka 
     42  1.1   pooka #include <rump/rump_syscalls.h>
     43  1.1   pooka 
     44  1.1   pooka #include "common.h"
     45  1.1   pooka #include "configure.h"
     46  1.1   pooka #include "dhcp.h"
     47  1.1   pooka #include "if-options.h"
     48  1.1   pooka #include "net.h"
     49  1.1   pooka 
     50  1.1   pooka /* Some systems have route metrics */
     51  1.1   pooka #ifndef HAVE_ROUTE_METRIC
     52  1.1   pooka # ifdef __linux__
     53  1.1   pooka #  define HAVE_ROUTE_METRIC 1
     54  1.1   pooka # endif
     55  1.1   pooka # ifndef HAVE_ROUTE_METRIC
     56  1.1   pooka #  define HAVE_ROUTE_METRIC 0
     57  1.1   pooka # endif
     58  1.1   pooka #endif
     59  1.1   pooka 
     60  1.1   pooka static struct rt *routes;
     61  1.1   pooka 
     62  1.1   pooka static struct rt *
     63  1.1   pooka find_route(struct rt *rts, const struct rt *r, struct rt **lrt,
     64  1.1   pooka     const struct rt *srt)
     65  1.1   pooka {
     66  1.1   pooka 	struct rt *rt;
     67  1.1   pooka 
     68  1.1   pooka 	if (lrt)
     69  1.1   pooka 		*lrt = NULL;
     70  1.1   pooka 	for (rt = rts; rt; rt = rt->next) {
     71  1.1   pooka 		if (rt->dest.s_addr == r->dest.s_addr &&
     72  1.1   pooka #if HAVE_ROUTE_METRIC
     73  1.1   pooka 		    (srt || (!rt->iface ||
     74  1.1   pooka 			rt->iface->metric == r->iface->metric)) &&
     75  1.1   pooka #endif
     76  1.1   pooka                     (!srt || srt != rt) &&
     77  1.1   pooka 		    rt->net.s_addr == r->net.s_addr)
     78  1.1   pooka 			return rt;
     79  1.1   pooka 		if (lrt)
     80  1.1   pooka 			*lrt = rt;
     81  1.1   pooka 	}
     82  1.1   pooka 	return NULL;
     83  1.1   pooka }
     84  1.1   pooka 
     85  1.1   pooka static void
     86  1.1   pooka desc_route(const char *cmd, const struct rt *rt, const char *ifname)
     87  1.1   pooka {
     88  1.1   pooka 	char addr[sizeof("000.000.000.000") + 1];
     89  1.1   pooka 
     90  1.1   pooka 	strlcpy(addr, inet_ntoa(rt->dest), sizeof(addr));
     91  1.1   pooka 	if (rt->gate.s_addr == INADDR_ANY)
     92  1.1   pooka 		fprintf(stderr, "%s: %s route to %s/%d\n", ifname, cmd,
     93  1.1   pooka 		    addr, inet_ntocidr(rt->net));
     94  1.1   pooka 	else if (rt->gate.s_addr == rt->dest.s_addr &&
     95  1.1   pooka 	    rt->net.s_addr == INADDR_BROADCAST)
     96  1.1   pooka 		fprintf(stderr, "%s: %s host route to %s\n", ifname, cmd,
     97  1.1   pooka 		    addr);
     98  1.1   pooka 	else if (rt->dest.s_addr == INADDR_ANY && rt->net.s_addr == INADDR_ANY)
     99  1.1   pooka 		fprintf(stderr, "%s: %s default route via %s\n", ifname, cmd,
    100  1.1   pooka 		    inet_ntoa(rt->gate));
    101  1.1   pooka 	else
    102  1.1   pooka 		fprintf(stderr, "%s: %s route to %s/%d via %s\n", ifname, cmd,
    103  1.1   pooka 		    addr, inet_ntocidr(rt->net), inet_ntoa(rt->gate));
    104  1.1   pooka }
    105  1.1   pooka 
    106  1.1   pooka /* If something other than dhcpcd removes a route,
    107  1.1   pooka  * we need to remove it from our internal table. */
    108  1.1   pooka int
    109  1.1   pooka route_deleted(const struct rt *rt)
    110  1.1   pooka {
    111  1.1   pooka 	struct rt *f, *l;
    112  1.1   pooka 
    113  1.1   pooka 	f = find_route(routes, rt, &l, NULL);
    114  1.1   pooka 	if (f == NULL)
    115  1.1   pooka 		return 0;
    116  1.1   pooka 	desc_route("removing", f, f->iface->name);
    117  1.1   pooka 	if (l)
    118  1.1   pooka 		l->next = f->next;
    119  1.1   pooka 	else
    120  1.1   pooka 		routes = f->next;
    121  1.1   pooka 	free(f);
    122  1.1   pooka 	return 1;
    123  1.1   pooka }
    124  1.1   pooka 
    125  1.1   pooka static int
    126  1.1   pooka n_route(struct rt *rt, const struct interface *iface)
    127  1.1   pooka {
    128  1.1   pooka 	/* Don't set default routes if not asked to */
    129  1.1   pooka 	if (rt->dest.s_addr == 0 &&
    130  1.1   pooka 	    rt->net.s_addr == 0 &&
    131  1.1   pooka 	    !(iface->state->options->options & DHCPCD_GATEWAY))
    132  1.1   pooka 		return -1;
    133  1.1   pooka 
    134  1.1   pooka 	desc_route("adding", rt, iface->name);
    135  1.1   pooka 	if (!add_route(iface, &rt->dest, &rt->net, &rt->gate, iface->metric))
    136  1.1   pooka 		return 0;
    137  1.1   pooka 	if (errno == EEXIST) {
    138  1.1   pooka 		/* Pretend we added the subnet route */
    139  1.1   pooka 		if (rt->dest.s_addr == (iface->addr.s_addr & iface->net.s_addr) &&
    140  1.1   pooka 		    rt->net.s_addr == iface->net.s_addr &&
    141  1.1   pooka 		    rt->gate.s_addr == 0)
    142  1.1   pooka 			return 0;
    143  1.1   pooka 		else
    144  1.1   pooka 			return -1;
    145  1.1   pooka 	}
    146  1.1   pooka 	fprintf(stderr, "%s: add_route failed: %d\n", iface->name, errno);
    147  1.1   pooka 	return -1;
    148  1.1   pooka }
    149  1.1   pooka 
    150  1.1   pooka static int
    151  1.1   pooka c_route(struct rt *ort, struct rt *nrt, const struct interface *iface)
    152  1.1   pooka {
    153  1.1   pooka 	/* Don't set default routes if not asked to */
    154  1.1   pooka 	if (nrt->dest.s_addr == 0 &&
    155  1.1   pooka 	    nrt->net.s_addr == 0 &&
    156  1.1   pooka 	    !(iface->state->options->options & DHCPCD_GATEWAY))
    157  1.1   pooka 		return -1;
    158  1.1   pooka 
    159  1.1   pooka 	desc_route("changing", nrt, iface->name);
    160  1.1   pooka 	/* We delete and add the route so that we can change metric.
    161  1.1   pooka 	 * This also has the nice side effect of flushing ARP entries so
    162  1.1   pooka 	 * we don't have to do that manually. */
    163  1.1   pooka 	del_route(ort->iface, &ort->dest, &ort->net, &ort->gate,
    164  1.1   pooka 	    ort->iface->metric);
    165  1.1   pooka 	if (!add_route(iface, &nrt->dest, &nrt->net, &nrt->gate,
    166  1.1   pooka 		iface->metric))
    167  1.1   pooka 		return 0;
    168  1.1   pooka 	fprintf(stderr, "%s: add_route failed: %d\n", iface->name, errno);
    169  1.1   pooka 	return -1;
    170  1.1   pooka }
    171  1.1   pooka 
    172  1.1   pooka static int
    173  1.1   pooka d_route(struct rt *rt, const struct interface *iface, int metric)
    174  1.1   pooka {
    175  1.1   pooka 	int retval;
    176  1.1   pooka 
    177  1.1   pooka 	desc_route("deleting", rt, iface->name);
    178  1.1   pooka 	retval = del_route(iface, &rt->dest, &rt->net, &rt->gate, metric);
    179  1.1   pooka 	if (retval != 0 && errno != ENOENT && errno != ESRCH)
    180  1.1   pooka 		fprintf(stderr,"%s: del_route: %d\n", iface->name, errno);
    181  1.1   pooka 	return retval;
    182  1.1   pooka }
    183  1.1   pooka 
    184  1.1   pooka static struct rt *
    185  1.1   pooka get_subnet_route(struct dhcp_message *dhcp)
    186  1.1   pooka {
    187  1.1   pooka 	in_addr_t addr;
    188  1.1   pooka 	struct in_addr net;
    189  1.1   pooka 	struct rt *rt;
    190  1.1   pooka 
    191  1.1   pooka 	addr = dhcp->yiaddr;
    192  1.1   pooka 	if (addr == 0)
    193  1.1   pooka 		addr = dhcp->ciaddr;
    194  1.1   pooka 	/* Ensure we have all the needed values */
    195  1.1   pooka 	if (get_option_addr(&net, dhcp, DHO_SUBNETMASK) == -1)
    196  1.1   pooka 		net.s_addr = get_netmask(addr);
    197  1.1   pooka 	if (net.s_addr == INADDR_BROADCAST || net.s_addr == INADDR_ANY)
    198  1.1   pooka 		return NULL;
    199  1.1   pooka 	rt = malloc(sizeof(*rt));
    200  1.1   pooka 	rt->dest.s_addr = addr & net.s_addr;
    201  1.1   pooka 	rt->net.s_addr = net.s_addr;
    202  1.1   pooka 	rt->gate.s_addr = 0;
    203  1.1   pooka 	return rt;
    204  1.1   pooka }
    205  1.1   pooka 
    206  1.1   pooka static struct rt *
    207  1.1   pooka add_subnet_route(struct rt *rt, const struct interface *iface)
    208  1.1   pooka {
    209  1.1   pooka 	struct rt *r;
    210  1.1   pooka 
    211  1.1   pooka 	if (iface->net.s_addr == INADDR_BROADCAST ||
    212  1.1   pooka 	    iface->net.s_addr == INADDR_ANY ||
    213  1.1   pooka 	    (iface->state->options->options &
    214  1.1   pooka 	     (DHCPCD_INFORM | DHCPCD_STATIC) &&
    215  1.1   pooka 	     iface->state->options->req_addr.s_addr == INADDR_ANY))
    216  1.1   pooka 		return rt;
    217  1.1   pooka 
    218  1.1   pooka 	r = xmalloc(sizeof(*r));
    219  1.1   pooka 	r->dest.s_addr = iface->addr.s_addr & iface->net.s_addr;
    220  1.1   pooka 	r->net.s_addr = iface->net.s_addr;
    221  1.1   pooka 	r->gate.s_addr = 0;
    222  1.1   pooka 	r->next = rt;
    223  1.1   pooka 	return r;
    224  1.1   pooka }
    225  1.1   pooka 
    226  1.1   pooka static struct rt *
    227  1.1   pooka get_routes(const struct interface *iface)
    228  1.1   pooka {
    229  1.1   pooka 	struct rt *rt, *nrt = NULL, *r = NULL;
    230  1.1   pooka 
    231  1.1   pooka 	if (iface->state->options->routes != NULL) {
    232  1.1   pooka 		for (rt = iface->state->options->routes;
    233  1.1   pooka 		     rt != NULL;
    234  1.1   pooka 		     rt = rt->next)
    235  1.1   pooka 		{
    236  1.1   pooka 			if (rt->gate.s_addr == 0)
    237  1.1   pooka 				break;
    238  1.1   pooka 			if (r == NULL)
    239  1.1   pooka 				r = nrt = xmalloc(sizeof(*r));
    240  1.1   pooka 			else {
    241  1.1   pooka 				r->next = xmalloc(sizeof(*r));
    242  1.1   pooka 				r = r->next;
    243  1.1   pooka 			}
    244  1.1   pooka 			memcpy(r, rt, sizeof(*r));
    245  1.1   pooka 			r->next = NULL;
    246  1.1   pooka 		}
    247  1.1   pooka 		return nrt;
    248  1.1   pooka 	}
    249  1.1   pooka 
    250  1.1   pooka 	return get_option_routes(iface->state->new,
    251  1.1   pooka 	    iface->name, &iface->state->options->options);
    252  1.1   pooka }
    253  1.1   pooka 
    254  1.1   pooka /* Some DHCP servers add set host routes by setting the gateway
    255  1.2  andvar  * to the assigned IP address. This differs from our notion of a host route
    256  1.1   pooka  * where the gateway is the destination address, so we fix it. */
    257  1.1   pooka static struct rt *
    258  1.1   pooka massage_host_routes(struct rt *rt, const struct interface *iface)
    259  1.1   pooka {
    260  1.1   pooka 	struct rt *r;
    261  1.1   pooka 
    262  1.1   pooka 	for (r = rt; r; r = r->next)
    263  1.1   pooka 		if (r->gate.s_addr == iface->addr.s_addr &&
    264  1.1   pooka 		    r->net.s_addr == INADDR_BROADCAST)
    265  1.1   pooka 			r->gate.s_addr = r->dest.s_addr;
    266  1.1   pooka 	return rt;
    267  1.1   pooka }
    268  1.1   pooka 
    269  1.1   pooka static struct rt *
    270  1.1   pooka add_destination_route(struct rt *rt, const struct interface *iface)
    271  1.1   pooka {
    272  1.1   pooka 	struct rt *r;
    273  1.1   pooka 
    274  1.1   pooka 	if (!(iface->flags & IFF_POINTOPOINT) ||
    275  1.1   pooka 	    !has_option_mask(iface->state->options->dstmask, DHO_ROUTER))
    276  1.1   pooka 		return rt;
    277  1.1   pooka 	r = xmalloc(sizeof(*r));
    278  1.1   pooka 	r->dest.s_addr = INADDR_ANY;
    279  1.1   pooka 	r->net.s_addr = INADDR_ANY;
    280  1.1   pooka 	r->gate.s_addr = iface->dst.s_addr;
    281  1.1   pooka 	r->next = rt;
    282  1.1   pooka 	return r;
    283  1.1   pooka }
    284  1.1   pooka 
    285  1.1   pooka /* We should check to ensure the routers are on the same subnet
    286  1.1   pooka  * OR supply a host route. If not, warn and add a host route. */
    287  1.1   pooka static struct rt *
    288  1.1   pooka add_router_host_route(struct rt *rt, const struct interface *ifp)
    289  1.1   pooka {
    290  1.1   pooka 	struct rt *rtp, *rtl, *rtn;
    291  1.1   pooka 	const char *cp, *cp2, *cp3, *cplim;
    292  1.1   pooka 
    293  1.1   pooka 	for (rtp = rt, rtl = NULL; rtp; rtl = rtp, rtp = rtp->next) {
    294  1.1   pooka 		if (rtp->dest.s_addr != INADDR_ANY)
    295  1.1   pooka 			continue;
    296  1.1   pooka 		/* Scan for a route to match */
    297  1.1   pooka 		for (rtn = rt; rtn != rtp; rtn = rtn->next) {
    298  1.1   pooka 			/* match host */
    299  1.1   pooka 			if (rtn->dest.s_addr == rtp->gate.s_addr)
    300  1.1   pooka 				break;
    301  1.1   pooka 			/* match subnet */
    302  1.1   pooka 			cp = (const char *)&rtp->gate.s_addr;
    303  1.1   pooka 			cp2 = (const char *)&rtn->dest.s_addr;
    304  1.1   pooka 			cp3 = (const char *)&rtn->net.s_addr;
    305  1.1   pooka 			cplim = cp3 + sizeof(rtn->net.s_addr);
    306  1.1   pooka 			while (cp3 < cplim) {
    307  1.1   pooka 				if ((*cp++ ^ *cp2++) & *cp3++)
    308  1.1   pooka 					break;
    309  1.1   pooka 			}
    310  1.1   pooka 			if (cp3 == cplim)
    311  1.1   pooka 				break;
    312  1.1   pooka 		}
    313  1.1   pooka 		if (rtn != rtp)
    314  1.1   pooka 			continue;
    315  1.1   pooka 		if (ifp->flags & IFF_NOARP) {
    316  1.1   pooka 			fprintf(stderr,
    317  1.1   pooka 			    "%s: forcing router %s through interface\n",
    318  1.1   pooka 			    ifp->name, inet_ntoa(rtp->gate));
    319  1.1   pooka 			rtp->gate.s_addr = 0;
    320  1.1   pooka 			continue;
    321  1.1   pooka 		}
    322  1.1   pooka 		fprintf(stderr, "%s: router %s requires a host route\n",
    323  1.1   pooka 		    ifp->name, inet_ntoa(rtp->gate));
    324  1.1   pooka 		rtn = xmalloc(sizeof(*rtn));
    325  1.1   pooka 		rtn->dest.s_addr = rtp->gate.s_addr;
    326  1.1   pooka 		rtn->net.s_addr = INADDR_BROADCAST;
    327  1.1   pooka 		rtn->gate.s_addr = rtp->gate.s_addr;
    328  1.1   pooka 		rtn->next = rtp;
    329  1.1   pooka 		if (rtl == NULL)
    330  1.1   pooka 			rt = rtn;
    331  1.1   pooka 		else
    332  1.1   pooka 			rtl->next = rtn;
    333  1.1   pooka 	}
    334  1.1   pooka 	return rt;
    335  1.1   pooka }
    336  1.1   pooka 
    337  1.1   pooka void
    338  1.1   pooka build_routes(void)
    339  1.1   pooka {
    340  1.1   pooka 	struct rt *nrs = NULL, *dnr, *or, *rt, *rtn, *rtl, *lrt = NULL;
    341  1.1   pooka 	const struct interface *ifp;
    342  1.1   pooka 
    343  1.1   pooka 	for (ifp = ifaces; ifp; ifp = ifp->next) {
    344  1.1   pooka 		if (ifp->state->new == NULL)
    345  1.1   pooka 			continue;
    346  1.1   pooka 		dnr = get_routes(ifp);
    347  1.1   pooka 		dnr = massage_host_routes(dnr, ifp);
    348  1.1   pooka 		dnr = add_subnet_route(dnr, ifp);
    349  1.1   pooka 		dnr = add_router_host_route(dnr, ifp);
    350  1.1   pooka 		dnr = add_destination_route(dnr, ifp);
    351  1.1   pooka 		for (rt = dnr; rt && (rtn = rt->next, 1); lrt = rt, rt = rtn) {
    352  1.1   pooka 			rt->iface = ifp;
    353  1.1   pooka 			/* Is this route already in our table? */
    354  1.1   pooka 			if ((find_route(nrs, rt, NULL, NULL)) != NULL)
    355  1.1   pooka 				continue;
    356  1.1   pooka 			/* Do we already manage it? */
    357  1.1   pooka 			if ((or = find_route(routes, rt, &rtl, NULL))) {
    358  1.1   pooka 				if (or->iface != ifp ||
    359  1.1   pooka 				    rt->gate.s_addr != or->gate.s_addr)
    360  1.1   pooka 				{
    361  1.1   pooka 					if (c_route(or, rt, ifp) != 0)
    362  1.1   pooka 						continue;
    363  1.1   pooka 				}
    364  1.1   pooka 				if (rtl != NULL)
    365  1.1   pooka 					rtl->next = or->next;
    366  1.1   pooka 				else
    367  1.1   pooka 					routes = or->next;
    368  1.1   pooka 				free(or);
    369  1.1   pooka 			} else {
    370  1.1   pooka 				if (n_route(rt, ifp) != 0)
    371  1.1   pooka 					continue;
    372  1.1   pooka 			}
    373  1.1   pooka 			if (dnr == rt)
    374  1.1   pooka 				dnr = rtn;
    375  1.1   pooka 			else if (lrt)
    376  1.1   pooka 				lrt->next = rtn;
    377  1.1   pooka 			rt->next = nrs;
    378  1.1   pooka 			nrs = rt;
    379  1.1   pooka 		}
    380  1.1   pooka 		free_routes(dnr);
    381  1.1   pooka 	}
    382  1.1   pooka 
    383  1.1   pooka 	/* Remove old routes we used to manage */
    384  1.1   pooka 	for (rt = routes; rt; rt = rt->next) {
    385  1.1   pooka 		if (find_route(nrs, rt, NULL, NULL) == NULL)
    386  1.1   pooka 			d_route(rt, rt->iface, rt->iface->metric);
    387  1.1   pooka 	}
    388  1.1   pooka 
    389  1.1   pooka 	free_routes(routes);
    390  1.1   pooka 	routes = nrs;
    391  1.1   pooka }
    392  1.1   pooka 
    393  1.1   pooka static int
    394  1.1   pooka delete_address(struct interface *iface)
    395  1.1   pooka {
    396  1.1   pooka 	int retval;
    397  1.1   pooka 	struct if_options *ifo;
    398  1.1   pooka 
    399  1.1   pooka 	ifo = iface->state->options;
    400  1.1   pooka 	if (ifo->options & DHCPCD_INFORM ||
    401  1.1   pooka 	    (ifo->options & DHCPCD_STATIC && ifo->req_addr.s_addr == 0))
    402  1.1   pooka 		return 0;
    403  1.1   pooka 	fprintf(stderr, "%s: deleting IP address %s/%d\n",
    404  1.1   pooka 	    iface->name,
    405  1.1   pooka 	    inet_ntoa(iface->addr),
    406  1.1   pooka 	    inet_ntocidr(iface->net));
    407  1.1   pooka 	retval = del_address(iface, &iface->addr, &iface->net);
    408  1.1   pooka 	if (retval == -1 && errno != EADDRNOTAVAIL)
    409  1.1   pooka 		fprintf(stderr, "del_address failed: %d\n", errno);
    410  1.1   pooka 	iface->addr.s_addr = 0;
    411  1.1   pooka 	iface->net.s_addr = 0;
    412  1.1   pooka 	return retval;
    413  1.1   pooka }
    414  1.1   pooka 
    415  1.1   pooka int
    416  1.1   pooka configure(struct interface *iface)
    417  1.1   pooka {
    418  1.1   pooka 	struct dhcp_message *dhcp = iface->state->new;
    419  1.1   pooka 	struct dhcp_lease *lease = &iface->state->lease;
    420  1.1   pooka 	struct if_options *ifo = iface->state->options;
    421  1.1   pooka 	struct rt *rt;
    422  1.1   pooka 
    423  1.1   pooka 	/* This also changes netmask */
    424  1.1   pooka 	if (!(ifo->options & DHCPCD_INFORM) ||
    425  1.1   pooka 	    !has_address(iface->name, &lease->addr, &lease->net))
    426  1.1   pooka 	{
    427  1.1   pooka 		fprintf(stderr, "%s: adding IP address %s/%d\n",
    428  1.1   pooka 		    iface->name, inet_ntoa(lease->addr),
    429  1.1   pooka 		    inet_ntocidr(lease->net));
    430  1.1   pooka 		if (add_address(iface,
    431  1.1   pooka 			&lease->addr, &lease->net, &lease->brd) == -1 &&
    432  1.1   pooka 		    errno != EEXIST)
    433  1.1   pooka 		{
    434  1.1   pooka 			fprintf(stderr, "add_address failed\n");
    435  1.1   pooka 			return -1;
    436  1.1   pooka 		}
    437  1.1   pooka 	}
    438  1.1   pooka 
    439  1.1   pooka 	/* Now delete the old address if different */
    440  1.1   pooka 	if (iface->addr.s_addr != lease->addr.s_addr &&
    441  1.1   pooka 	    iface->addr.s_addr != 0)
    442  1.1   pooka 		delete_address(iface);
    443  1.1   pooka 
    444  1.1   pooka 	iface->addr.s_addr = lease->addr.s_addr;
    445  1.1   pooka 	iface->net.s_addr = lease->net.s_addr;
    446  1.1   pooka 
    447  1.1   pooka 	/* We need to delete the subnet route to have our metric or
    448  1.1   pooka 	 * prefer the interface. */
    449  1.1   pooka 	rt = get_subnet_route(dhcp);
    450  1.1   pooka 	if (rt != NULL) {
    451  1.1   pooka 		rt->iface = iface;
    452  1.1   pooka 		if (!find_route(routes, rt, NULL, NULL))
    453  1.1   pooka 			del_route(iface, &rt->dest, &rt->net, &rt->gate, 0);
    454  1.1   pooka 		free(rt);
    455  1.1   pooka 	}
    456  1.1   pooka 
    457  1.1   pooka 	build_routes();
    458  1.1   pooka 
    459  1.1   pooka 	fprintf(stderr, "lease time: ");
    460  1.1   pooka 	if (lease->leasetime == ~0U)
    461  1.1   pooka 		fprintf(stderr, "infinite\n");
    462  1.1   pooka 	else
    463  1.1   pooka 		fprintf(stderr, "%u seconds (%.2f days)\n",
    464  1.1   pooka 		    lease->leasetime, lease->leasetime / (60*60*24+.0));
    465  1.1   pooka 
    466  1.1   pooka 	return 0;
    467  1.1   pooka }
    468