1/*
2 * dhcpcd - DHCP client daemon
3 * Copyright (c) 2006-2012 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/types.h>
29#include <sys/socket.h>
30
31#include <netinet/in.h>
32
33#include <errno.h>
34#include <ifaddrs.h>
35#include <stdlib.h>
36#include <string.h>
37#include <syslog.h>
38
39#include "common.h"
40#include "configure.h"
41#include "dhcpcd.h"
42#include "ipv6.h"
43#include "ipv6rs.h"
44
45/* Hackery at it's finest. */
46#ifndef s6_addr32
47#  define s6_addr32 __u6_addr.__u6_addr32
48#endif
49
50int socket_afnet6;
51static struct rt6head *routes;
52
53#ifdef DEBUG_MEMORY
54static void
55ipv6_cleanup()
56{
57	struct rt6 *rt;
58
59	while ((rt = TAILQ_FIRST(routes))) {
60		TAILQ_REMOVE(routes, rt, next);
61		free(rt);
62	}
63	free(routes);
64}
65#endif
66
67int
68ipv6_open(void)
69{
70	socket_afnet6 = socket(AF_INET6, SOCK_DGRAM, 0);
71	if (socket_afnet6 == -1)
72		return -1;
73	set_cloexec(socket_afnet6);
74	routes = xmalloc(sizeof(*routes));
75	TAILQ_INIT(routes);
76#ifdef DEBUG_MEMORY
77	atexit(ipv6_cleanup);
78#endif
79	return socket_afnet6;
80}
81
82struct in6_addr *
83ipv6_linklocal(const char *ifname)
84{
85	struct ifaddrs *ifaddrs, *ifa;
86	struct sockaddr_in6 *sa6;
87	struct in6_addr *in6;
88
89	if (getifaddrs(&ifaddrs) == -1)
90		return NULL;
91
92	for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
93		if (ifa->ifa_addr == NULL ||
94		    ifa->ifa_addr->sa_family != AF_INET6)
95			continue;
96		if (strcmp(ifa->ifa_name, ifname))
97			continue;
98		sa6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr;
99		if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr))
100			break;
101	}
102
103	if (ifa) {
104		in6 = xmalloc(sizeof(*in6));
105		memcpy(in6, &sa6->sin6_addr, sizeof(*in6));
106	} else
107		in6 = NULL;
108
109	freeifaddrs(ifaddrs);
110	return in6;
111}
112
113int
114ipv6_makeaddr(struct in6_addr *addr, const char *ifname,
115    const struct in6_addr *prefix, int prefix_len)
116{
117	struct in6_addr *lla;
118
119	if (prefix_len > 64) {
120		errno = EINVAL;
121		return -1;
122	}
123
124	lla = ipv6_linklocal(ifname);
125	if (lla == NULL) {
126		errno = ENOENT;
127		return -1;
128	}
129
130	memcpy(addr, prefix, sizeof(*prefix));
131	addr->s6_addr32[2] = lla->s6_addr32[2];
132	addr->s6_addr32[3] = lla->s6_addr32[3];
133	free(lla);
134	return 0;
135}
136
137int
138ipv6_mask(struct in6_addr *mask, int len)
139{
140	static const unsigned char masks[NBBY] =
141	    { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
142	int bytes, bits, i;
143
144	if (len < 0 || len > 128) {
145		errno = EINVAL;
146		return -1;
147	}
148
149	memset(mask, 0, sizeof(*mask));
150	bytes = len / NBBY;
151	bits = len % NBBY;
152	for (i = 0; i < bytes; i++)
153		mask->s6_addr[i] = 0xff;
154	if (bits)
155		mask->s6_addr[bytes] = masks[bits - 1];
156	return 0;
157}
158
159int
160ipv6_prefixlen(const struct in6_addr *mask)
161{
162	int x = 0, y;
163	const unsigned char *lim, *p;
164
165	lim = (const unsigned char *)mask + sizeof(*mask);
166	for (p = (const unsigned char *)mask; p < lim; x++, p++) {
167		if (*p != 0xff)
168			break;
169	}
170	y = 0;
171	if (p < lim) {
172		for (y = 0; y < NBBY; y++) {
173			if ((*p & (0x80 >> y)) == 0)
174				break;
175		}
176	}
177
178	/*
179	 * when the limit pointer is given, do a stricter check on the
180	 * remaining bits.
181	 */
182	if (p < lim) {
183		if (y != 0 && (*p & (0x00ff >> y)) != 0)
184			return -1;
185		for (p = p + 1; p < lim; p++)
186			if (*p != 0)
187				return -1;
188	}
189
190	return x * NBBY + y;
191}
192
193static struct rt6 *
194find_route6(struct rt6head *rts, const struct rt6 *r)
195{
196	struct rt6 *rt;
197
198	TAILQ_FOREACH(rt, rts, next) {
199		if (IN6_ARE_ADDR_EQUAL(&rt->dest, &r->dest) &&
200#if HAVE_ROUTE_METRIC
201		    rt->iface->metric == r->iface->metric &&
202#endif
203		    IN6_ARE_ADDR_EQUAL(&rt->net, &r->net))
204			return rt;
205	}
206	return NULL;
207}
208
209static void
210desc_route(const char *cmd, const struct rt6 *rt)
211{
212	char destbuf[INET6_ADDRSTRLEN];
213	char gatebuf[INET6_ADDRSTRLEN];
214	const char *ifname = rt->iface->name, *dest, *gate;
215
216	dest = inet_ntop(AF_INET6, &rt->dest.s6_addr,
217	    destbuf, INET6_ADDRSTRLEN);
218	gate = inet_ntop(AF_INET6, &rt->gate.s6_addr,
219	    gatebuf, INET6_ADDRSTRLEN);
220	if (IN6_ARE_ADDR_EQUAL(&rt->gate, &in6addr_any))
221		syslog(LOG_DEBUG, "%s: %s route to %s/%d", ifname, cmd,
222		    dest, ipv6_prefixlen(&rt->net));
223	else if (IN6_ARE_ADDR_EQUAL(&rt->dest, &in6addr_any) &&
224	    IN6_ARE_ADDR_EQUAL(&rt->net, &in6addr_any))
225		syslog(LOG_DEBUG, "%s: %s default route via %s", ifname, cmd,
226		    gate);
227	else
228		syslog(LOG_DEBUG, "%s: %s route to %s/%d via %s", ifname, cmd,
229		    dest, ipv6_prefixlen(&rt->net), gate);
230}
231
232static int
233n_route(struct rt6 *rt)
234{
235
236	/* Don't set default routes if not asked to */
237	if (IN6_IS_ADDR_UNSPECIFIED(&rt->dest) &&
238	    IN6_IS_ADDR_UNSPECIFIED(&rt->net) &&
239	    !(rt->iface->state->options->options & DHCPCD_GATEWAY))
240		return -1;
241
242	/* Delete the route first as it could exist prior to dhcpcd running
243	 * and we need to ensure it leaves via our preffered interface */
244	del_route6(rt);
245	desc_route("adding", rt);
246	if (!add_route6(rt))
247		return 0;
248
249	syslog(LOG_ERR, "%s: add_route: %m", rt->iface->name);
250	return -1;
251}
252
253static int
254c_route(struct rt6 *ort, struct rt6 *nrt)
255{
256
257	/* Don't set default routes if not asked to */
258	if (IN6_IS_ADDR_UNSPECIFIED(&nrt->dest) &&
259	    IN6_IS_ADDR_UNSPECIFIED(&nrt->net) &&
260	    !(nrt->iface->state->options->options & DHCPCD_GATEWAY))
261		return -1;
262
263	desc_route("changing", nrt);
264	/* We delete and add the route so that we can change metric.
265	 * This also has the nice side effect of flushing ARP entries so
266	 * we don't have to do that manually. */
267	del_route6(ort);
268	if (!add_route6(nrt))
269		return 0;
270	syslog(LOG_ERR, "%s: add_route: %m", nrt->iface->name);
271	return -1;
272}
273
274static int
275d_route(struct rt6 *rt)
276{
277	int retval;
278
279	desc_route("deleting", rt);
280	retval = del_route6(rt);
281	if (retval != 0 && errno != ENOENT && errno != ESRCH)
282		syslog(LOG_ERR,"%s: del_route: %m", rt->iface->name);
283	return retval;
284}
285
286static struct rt6 *
287make_route(struct ra *rap)
288{
289	struct rt6 *r;
290
291	r = xzalloc(sizeof(*r));
292	r->ra = rap;
293	r->iface = rap->iface;
294	r->metric = rap->iface->metric;
295	r->mtu = rap->mtu;
296	return r;
297}
298
299static struct rt6 *
300make_prefix(struct ra *rap, struct ipv6_addr *addr)
301{
302	struct rt6 *r;
303
304	if (addr == NULL || addr->prefix_len > 128)
305		return NULL;
306
307	r = make_route(rap);
308	r->dest = addr->prefix;
309	ipv6_mask(&r->net, addr->prefix_len);
310	r->gate = in6addr_any;
311	return r;
312}
313
314static struct rt6 *
315make_router(struct ra *rap)
316{
317	struct rt6 *r;
318
319	r = make_route(rap);
320	r->dest = in6addr_any;
321	r->net = in6addr_any;
322	r->gate = rap->from;
323	return r;
324}
325
326int
327ipv6_remove_subnet(struct ra *rap, struct ipv6_addr *addr)
328{
329	struct rt6 *rt;
330#if HAVE_ROUTE_METRIC
331	struct rt6 *ort;
332#endif
333	int r;
334
335	/* We need to delete the subnet route to have our metric or
336	 * prefer the interface. */
337	r = 0;
338	rt = make_prefix(rap, addr);
339	if (rt) {
340		rt->iface = rap->iface;
341#ifdef __linux__
342		rt->metric = 256;
343#else
344		rt->metric = 0;
345#endif
346#if HAVE_ROUTE_METRIC
347		/* For some reason, Linux likes to re-add the subnet
348		   route under the original metric.
349		   I would love to find a way of stopping this! */
350		if ((ort = find_route6(routes, rt)) == NULL ||
351		    ort->metric != rt->metric)
352#else
353		if (!find_route6(routes, rt))
354#endif
355			r = del_route6(rt);
356		free(rt);
357	}
358	return r;
359}
360
361#define RT_IS_DEFAULT(rtp) \
362	(IN6_ARE_ADDR_EQUAL(&((rtp)->dest), &in6addr_any) &&		      \
363	    IN6_ARE_ADDR_EQUAL(&((rtp)->net), &in6addr_any))
364
365void
366ipv6_build_routes(void)
367{
368	struct rt6head dnr, *nrs;
369	struct rt6 *rt, *rtn, *or;
370	struct ra *rap, *ran;
371	struct ipv6_addr *addr;
372	int have_default;
373
374	if (!(options & (DHCPCD_IPV6RA_OWN | DHCPCD_IPV6RA_OWN_DEFAULT)))
375		return;
376
377	TAILQ_INIT(&dnr);
378	TAILQ_FOREACH(rap, &ipv6_routers, next) {
379		if (rap->expired)
380			continue;
381		if (options & DHCPCD_IPV6RA_OWN) {
382			TAILQ_FOREACH(addr, &rap->addrs, next) {
383				rt = make_prefix(rap, addr);
384				if (rt)
385					TAILQ_INSERT_TAIL(&dnr, rt, next);
386			}
387		}
388		rt = make_router(rap);
389		if (rt)
390			TAILQ_INSERT_TAIL(&dnr, rt, next);
391	}
392
393	nrs = xmalloc(sizeof(*nrs));
394	TAILQ_INIT(nrs);
395	have_default = 0;
396	TAILQ_FOREACH_SAFE(rt, &dnr, next, rtn) {
397		/* Is this route already in our table? */
398		if (find_route6(nrs, rt) != NULL)
399			continue;
400		//rt->src.s_addr = ifp->addr.s_addr;
401		/* Do we already manage it? */
402		if ((or = find_route6(routes, rt))) {
403			if (or->iface != rt->iface ||
404		//	    or->src.s_addr != ifp->addr.s_addr ||
405			    !IN6_ARE_ADDR_EQUAL(&rt->gate, &or->gate) ||
406			    rt->metric != or->metric)
407			{
408				if (c_route(or, rt) != 0)
409					continue;
410			}
411			TAILQ_REMOVE(routes, or, next);
412			free(or);
413		} else {
414			if (n_route(rt) != 0)
415				continue;
416		}
417		if (RT_IS_DEFAULT(rt))
418			have_default = 1;
419		TAILQ_REMOVE(&dnr, rt, next);
420		TAILQ_INSERT_TAIL(nrs, rt, next);
421	}
422
423	/* Free any routes we failed to add/change */
424	while ((rt = TAILQ_FIRST(&dnr))) {
425		TAILQ_REMOVE(&dnr, rt, next);
426		free(rt);
427	}
428
429	/* Remove old routes we used to manage
430	 * If we own the default route, but not RA management itself
431	 * then we need to preserve the last best default route we had */
432	TAILQ_FOREACH_SAFE(rt, routes, next, rtn) {
433		if (find_route6(nrs, rt) == NULL) {
434			if (!have_default &&
435			    (options & DHCPCD_IPV6RA_OWN_DEFAULT) &&
436			    !(options & DHCPCD_IPV6RA_OWN) &&
437			    RT_IS_DEFAULT(rt))
438				have_default = 1;
439				/* no need to add it back to our routing table
440				 * as we delete an exiting route when we add
441				 * a new one */
442			else
443				d_route(rt);
444		}
445		free(rt);
446	}
447	free(routes);
448	routes = nrs;
449
450	/* Now drop expired routers */
451	TAILQ_FOREACH_SAFE(rap, &ipv6_routers, next, ran) {
452		if (rap->expired)
453			ipv6rs_drop_ra(rap);
454	}
455}
456