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