GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/slaacd/slaacd.c Lines: 241 410 58.8 %
Date: 2017-11-13 Branches: 76 188 40.4 %

Line Branch Exec Source
1
/*	$OpenBSD: slaacd.c,v 1.12 2017/11/03 12:28:41 florian Exp $	*/
2
3
/*
4
 * Copyright (c) 2017 Florian Obser <florian@openbsd.org>
5
 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
6
 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
7
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8
 *
9
 * Permission to use, copy, modify, and distribute this software for any
10
 * purpose with or without fee is hereby granted, provided that the above
11
 * copyright notice and this permission notice appear in all copies.
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
 */
21
#include <sys/types.h>
22
#include <sys/ioctl.h>
23
#include <sys/queue.h>
24
#include <sys/socket.h>
25
#include <sys/syslog.h>
26
#include <sys/uio.h>
27
#include <sys/wait.h>
28
29
#include <net/if.h>
30
#include <net/route.h>
31
#include <netinet/in.h>
32
#include <netinet/if_ether.h>
33
#include <netinet6/in6_var.h>
34
35
#include <err.h>
36
#include <errno.h>
37
#include <event.h>
38
#include <imsg.h>
39
#include <netdb.h>
40
#include <pwd.h>
41
#include <stddef.h>
42
#include <stdio.h>
43
#include <stdlib.h>
44
#include <string.h>
45
#include <signal.h>
46
#include <unistd.h>
47
48
#include "log.h"
49
#include "slaacd.h"
50
#include "frontend.h"
51
#include "engine.h"
52
#include "control.h"
53
54
#ifndef	SMALL
55
const char* imsg_type_name[] = {
56
	"IMSG_NONE",
57
	"IMSG_CTL_LOG_VERBOSE",
58
	"IMSG_CTL_SHOW_INTERFACE_INFO",
59
	"IMSG_CTL_SHOW_INTERFACE_INFO_RA",
60
	"IMSG_CTL_SHOW_INTERFACE_INFO_RA_PREFIX",
61
	"IMSG_CTL_SHOW_INTERFACE_INFO_RA_RDNS",
62
	"IMSG_CTL_SHOW_INTERFACE_INFO_RA_DNSSL",
63
	"IMSG_CTL_SHOW_INTERFACE_INFO_ADDR_PROPOSALS",
64
	"IMSG_CTL_SHOW_INTERFACE_INFO_ADDR_PROPOSAL",
65
	"IMSG_CTL_SHOW_INTERFACE_INFO_DFR_PROPOSALS",
66
	"IMSG_CTL_SHOW_INTERFACE_INFO_DFR_PROPOSAL",
67
	"IMSG_CTL_END",
68
	"IMSG_UPDATE_ADDRESS",
69
	"IMSG_CTL_SEND_SOLICITATION",
70
	"IMSG_SOCKET_IPC",
71
	"IMSG_STARTUP",
72
	"IMSG_UPDATE_IF",
73
	"IMSG_REMOVE_IF",
74
	"IMSG_RA",
75
	"IMSG_PROPOSAL",
76
	"IMSG_PROPOSAL_ACK",
77
	"IMSG_CONFIGURE_ADDRESS",
78
	"IMSG_DEL_ADDRESS",
79
	"IMSG_FAKE_ACK",
80
	"IMSG_CONFIGURE_DFR",
81
	"IMSG_WITHDRAW_DFR",
82
};
83
#endif	/* SMALL */
84
85
__dead void	usage(void);
86
__dead void	main_shutdown(void);
87
88
void	main_sig_handler(int, short, void *);
89
90
static pid_t	start_child(int, char *, int, int, int, char *);
91
92
void	main_dispatch_frontend(int, short, void *);
93
void	main_dispatch_engine(int, short, void *);
94
void	handle_proposal(struct imsg_proposal *);
95
void	configure_interface(struct imsg_configure_address *);
96
void	configure_gateway(struct imsg_configure_dfr *, uint8_t);
97
void	add_gateway(struct imsg_configure_dfr *);
98
void	delete_gateway(struct imsg_configure_dfr *);
99
100
static int	main_imsg_send_ipc_sockets(struct imsgbuf *, struct imsgbuf *);
101
102
struct imsgev		*iev_frontend;
103
struct imsgev		*iev_engine;
104
105
pid_t	 frontend_pid;
106
pid_t	 engine_pid;
107
108
int	 routesock, ioctl_sock;
109
110
char	*csock;
111
112
int	 rtm_seq = 0;
113
114
void
115
main_sig_handler(int sig, short event, void *arg)
116
{
117
	/*
118
	 * Normal signal handler rules don't apply because libevent
119
	 * decouples for us.
120
	 */
121
122

24
	switch (sig) {
123
	case SIGTERM:
124
	case SIGINT:
125
		main_shutdown();
126
	case SIGHUP:
127
		log_debug("sighub received");
128
		break;
129
	default:
130
		fatalx("unexpected signal");
131
	}
132
}
133
134
__dead void
135
usage(void)
136
{
137
	extern char *__progname;
138
139
	fprintf(stderr, "usage: %s [-dv] [-s socket]\n",
140
	    __progname);
141
	exit(1);
142
}
143
144
int
145
main(int argc, char *argv[])
146
{
147
24
	struct event	 ev_sigint, ev_sigterm, ev_sighup;
148
	int		 ch;
149
	int		 debug = 0, engine_flag = 0, frontend_flag = 0;
150
	int		 verbose = 0;
151
	char		*saved_argv0;
152
12
	int		 pipe_main2frontend[2];
153
12
	int		 pipe_main2engine[2];
154
155
12
	csock = SLAACD_SOCKET;
156
157
12
	log_init(1, LOG_DAEMON);	/* Log to stderr until daemonized. */
158
12
	log_setverbose(1);
159
160
12
	saved_argv0 = argv[0];
161
12
	if (saved_argv0 == NULL)
162
		saved_argv0 = "slaacd";
163
164
48
	while ((ch = getopt(argc, argv, "dEFs:v")) != -1) {
165

12
		switch (ch) {
166
		case 'd':
167
			debug = 1;
168
			break;
169
		case 'E':
170
			engine_flag = 1;
171
			break;
172
		case 'F':
173
			frontend_flag = 1;
174
			break;
175
		case 's':
176
12
			csock = optarg;
177
12
			break;
178
		case 'v':
179
			verbose++;
180
			break;
181
		default:
182
			usage();
183
		}
184
	}
185
186
12
	argc -= optind;
187
12
	argv += optind;
188

24
	if (argc > 0 || (engine_flag && frontend_flag))
189
		usage();
190
191
12
	if (engine_flag)
192
		engine(debug, verbose);
193
12
	else if (frontend_flag)
194
		frontend(debug, verbose, csock);
195
196
	/* Check for root privileges. */
197
12
	if (geteuid())
198
		errx(1, "need root privileges");
199
200
	/* Check for assigned daemon user */
201
12
	if (getpwnam(SLAACD_USER) == NULL)
202
		errx(1, "unknown user %s", SLAACD_USER);
203
204
12
	log_init(debug, LOG_DAEMON);
205
12
	log_setverbose(verbose);
206
207
12
	if (!debug)
208
12
		daemon(1, 0);
209
210
12
	log_info("startup");
211
212
24
	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
213
24
	    PF_UNSPEC, pipe_main2frontend) == -1)
214
		fatal("main2frontend socketpair");
215
24
	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
216
24
	    PF_UNSPEC, pipe_main2engine) == -1)
217
		fatal("main2engine socketpair");
218
219
	/* Start children. */
220
12
	engine_pid = start_child(PROC_ENGINE, saved_argv0, pipe_main2engine[1],
221
	    debug, verbose, NULL);
222
12
	frontend_pid = start_child(PROC_FRONTEND, saved_argv0,
223
12
	    pipe_main2frontend[1], debug, verbose, csock);
224
225
12
	slaacd_process = PROC_MAIN;
226
227
12
	log_procinit(log_procnames[slaacd_process]);
228
229
24
	if ((routesock = socket(PF_ROUTE, SOCK_RAW | SOCK_CLOEXEC |
230
12
	    SOCK_NONBLOCK, 0)) < 0)
231
		fatal("route socket");
232
233
12
	event_init();
234
235
	/* Setup signal handler. */
236
12
	signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
237
12
	signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
238
12
	signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
239
12
	signal_add(&ev_sigint, NULL);
240
12
	signal_add(&ev_sigterm, NULL);
241
12
	signal_add(&ev_sighup, NULL);
242
12
	signal(SIGPIPE, SIG_IGN);
243
244
	/* Setup pipes to children. */
245
246

24
	if ((iev_frontend = malloc(sizeof(struct imsgev))) == NULL ||
247
12
	    (iev_engine = malloc(sizeof(struct imsgev))) == NULL)
248
		fatal(NULL);
249
12
	imsg_init(&iev_frontend->ibuf, pipe_main2frontend[0]);
250
12
	iev_frontend->handler = main_dispatch_frontend;
251
12
	imsg_init(&iev_engine->ibuf, pipe_main2engine[0]);
252
12
	iev_engine->handler = main_dispatch_engine;
253
254
	/* Setup event handlers for pipes to engine & frontend. */
255
12
	iev_frontend->events = EV_READ;
256
24
	event_set(&iev_frontend->ev, iev_frontend->ibuf.fd,
257
12
	    iev_frontend->events, iev_frontend->handler, iev_frontend);
258
12
	event_add(&iev_frontend->ev, NULL);
259
260
12
	iev_engine->events = EV_READ;
261
24
	event_set(&iev_engine->ev, iev_engine->ibuf.fd, iev_engine->events,
262
12
	    iev_engine->handler, iev_engine);
263
12
	event_add(&iev_engine->ev, NULL);
264
265
12
	if (main_imsg_send_ipc_sockets(&iev_frontend->ibuf, &iev_engine->ibuf))
266
		fatal("could not establish imsg links");
267
268
12
	if ((ioctl_sock = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)) < 0)
269
		fatal("socket");
270
271
#if 0
272
	/* XXX ioctl SIOCAIFADDR_IN6 */
273
BROKEN	if (pledge("rpath stdio sendfd cpath flock wpath", NULL) == -1)
274
		fatal("pledge");
275
#endif
276
277
	main_imsg_compose_frontend(IMSG_STARTUP, 0, NULL, 0);
278
279
	event_dispatch();
280
281
	main_shutdown();
282
	return (0);
283
}
284
285
__dead void
286
main_shutdown(void)
287
{
288
	pid_t	 pid;
289
24
	int	 status;
290
291
	/* Close pipes. */
292
12
	msgbuf_clear(&iev_frontend->ibuf.w);
293
12
	close(iev_frontend->ibuf.fd);
294
12
	msgbuf_clear(&iev_engine->ibuf.w);
295
12
	close(iev_engine->ibuf.fd);
296
297
12
	log_debug("waiting for children to terminate");
298
12
	do {
299
36
		pid = wait(&status);
300
36
		if (pid == -1) {
301

24
			if (errno != EINTR && errno != ECHILD)
302
				fatal("wait");
303

48
		} else if (WIFSIGNALED(status))
304
			log_warnx("%s terminated; signal %d",
305
			    (pid == engine_pid) ? "engine" :
306
			    "frontend", WTERMSIG(status));
307

60
	} while (pid != -1 || (pid == -1 && errno == EINTR));
308
309
	free(iev_frontend);
310
	free(iev_engine);
311
312
#ifndef	SMALL
313
	control_cleanup(csock);
314
#endif	/* SMALL */
315
316
	log_info("terminating");
317
	exit(0);
318
}
319
320
static pid_t
321
start_child(int p, char *argv0, int fd, int debug, int verbose, char *sockname)
322
{
323
48
	char	*argv[8];
324
	int	 argc = 0;
325
	pid_t	 pid;
326
327
24
	switch (pid = fork()) {
328
	case -1:
329
		fatal("cannot fork");
330
	case 0:
331
		break;
332
	default:
333
24
		close(fd);
334
24
		return (pid);
335
	}
336
337
	if (dup2(fd, 3) == -1)
338
		fatal("cannot setup imsg fd");
339
340
	argv[argc++] = argv0;
341
	switch (p) {
342
	case PROC_MAIN:
343
		fatalx("Can not start main process");
344
	case PROC_ENGINE:
345
		argv[argc++] = "-E";
346
		break;
347
	case PROC_FRONTEND:
348
		argv[argc++] = "-F";
349
		break;
350
	}
351
	if (debug)
352
		argv[argc++] = "-d";
353
	if (verbose)
354
		argv[argc++] = "-v";
355
	if (verbose > 1)
356
		argv[argc++] = "-v";
357
	if (sockname) {
358
		argv[argc++] = "-s";
359
		argv[argc++] = sockname;
360
	}
361
	argv[argc++] = NULL;
362
363
	execvp(argv0, argv);
364
	fatal("execvp");
365
24
}
366
367
void
368
main_dispatch_frontend(int fd, short event, void *bula)
369
{
370
60
	struct imsgev		*iev = bula;
371
	struct imsgbuf		*ibuf;
372
30
	struct imsg		 imsg;
373
30
	struct imsg_ifinfo	 imsg_ifinfo;
374
	ssize_t			 n;
375
	int			 shut = 0;
376
#ifndef	SMALL
377
30
	struct imsg_addrinfo	 imsg_addrinfo;
378
	int			 verbose;
379
#endif	/* SMALL */
380
381
30
	ibuf = &iev->ibuf;
382
383
30
	if (event & EV_READ) {
384

18
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
385
			fatal("imsg_read error");
386
18
		if (n == 0)	/* Connection closed. */
387
			shut = 1;
388
	}
389
30
	if (event & EV_WRITE) {
390

12
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
391
			fatal("msgbuf_write");
392
12
		if (n == 0)	/* Connection closed. */
393
			shut = 1;
394
	}
395
396
54
	for (;;) {
397
54
		if ((n = imsg_get(ibuf, &imsg)) == -1)
398
			fatal("imsg_get");
399
54
		if (n == 0)	/* No more messages. */
400
			break;
401
402

24
		switch (imsg.hdr.type) {
403
#ifndef	SMALL
404
		case IMSG_CTL_LOG_VERBOSE:
405
			/* Already checked by frontend. */
406
			memcpy(&verbose, imsg.data, sizeof(verbose));
407
			log_setverbose(verbose);
408
			break;
409
		case IMSG_UPDATE_ADDRESS:
410
			if (imsg.hdr.len != IMSG_HEADER_SIZE +
411
			    sizeof(imsg_addrinfo))
412
				fatal("%s: IMSG_UPDATE_ADDRESS wrong length: "
413
				    "%d", __func__, imsg.hdr.len);
414
			memcpy(&imsg_addrinfo, imsg.data,
415
			    sizeof(imsg_addrinfo));
416
			main_imsg_compose_engine(IMSG_UPDATE_ADDRESS, 0,
417
			    &imsg_addrinfo, sizeof(imsg_addrinfo));
418
			break;
419
#endif	/* SMALL */
420
		case IMSG_UPDATE_IF:
421
24
			if (imsg.hdr.len != IMSG_HEADER_SIZE +
422
			    sizeof(imsg_ifinfo))
423
				fatal("%s: IMSG_UPDATE_IF wrong length: %d",
424
				    __func__, imsg.hdr.len);
425
24
			memcpy(&imsg_ifinfo, imsg.data, sizeof(imsg_ifinfo));
426
24
			main_imsg_compose_engine(IMSG_UPDATE_IF, 0,
427
			    &imsg_ifinfo, sizeof(imsg_ifinfo));
428
24
			break;
429
		default:
430
			log_debug("%s: error handling imsg %d", __func__,
431
			    imsg.hdr.type);
432
			break;
433
		}
434
24
		imsg_free(&imsg);
435
	}
436
30
	if (!shut)
437
30
		imsg_event_add(iev);
438
	else {
439
		/* This pipe is dead. Remove its event handler */
440
		event_del(&iev->ev);
441
		event_loopexit(NULL);
442
	}
443
30
}
444
445
void
446
main_dispatch_engine(int fd, short event, void *bula)
447
{
448
48
	struct imsgev			*iev = bula;
449
	struct imsgbuf			*ibuf;
450
24
	struct imsg			 imsg;
451
24
	struct imsg_proposal		 proposal;
452
24
	struct imsg_configure_address	 address;
453
24
	struct imsg_configure_dfr	 dfr;
454
	ssize_t				 n;
455
	int				 shut = 0;
456
457
24
	ibuf = &iev->ibuf;
458
459
24
	if (event & EV_READ) {
460

6
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
461
			fatal("imsg_read error");
462
6
		if (n == 0)	/* Connection closed. */
463
			shut = 1;
464
	}
465
24
	if (event & EV_WRITE) {
466

18
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
467
			fatal("msgbuf_write");
468
18
		if (n == 0)	/* Connection closed. */
469
			shut = 1;
470
	}
471
472
42
	for (;;) {
473
42
		if ((n = imsg_get(ibuf, &imsg)) == -1)
474
			fatal("imsg_get");
475
42
		if (n == 0)	/* No more messages. */
476
			break;
477
478

18
		switch (imsg.hdr.type) {
479
		case IMSG_PROPOSAL:
480
			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(proposal))
481
				fatal("%s: IMSG_PROPOSAL wrong "
482
				    "length: %d", __func__, imsg.hdr.len);
483
			memcpy(&proposal, imsg.data, sizeof(proposal));
484
			handle_proposal(&proposal);
485
			break;
486
		case IMSG_CONFIGURE_ADDRESS:
487
12
			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(address))
488
				fatal("%s: IMSG_CONFIGURE_ADDRESS wrong "
489
				    "length: %d", __func__, imsg.hdr.len);
490
12
			memcpy(&address, imsg.data, sizeof(address));
491
12
			configure_interface(&address);
492
12
			break;
493
		case IMSG_CONFIGURE_DFR:
494
6
			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(dfr))
495
				fatal("%s: IMSG_CONFIGURE_DFR wrong "
496
				    "length: %d", __func__, imsg.hdr.len);
497
6
			memcpy(&dfr, imsg.data, sizeof(dfr));
498
6
			add_gateway(&dfr);
499
6
			break;
500
		case IMSG_WITHDRAW_DFR:
501
			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(dfr))
502
				fatal("%s: IMSG_CONFIGURE_DFR wrong "
503
				    "length: %d", __func__, imsg.hdr.len);
504
			memcpy(&dfr, imsg.data, sizeof(dfr));
505
			delete_gateway(&dfr);
506
			break;
507
		default:
508
			log_debug("%s: error handling imsg %d", __func__,
509
			    imsg.hdr.type);
510
			break;
511
		}
512
18
		imsg_free(&imsg);
513
	}
514
24
	if (!shut)
515
24
		imsg_event_add(iev);
516
	else {
517
		/* This pipe is dead. Remove its event handler. */
518
		event_del(&iev->ev);
519
		event_loopexit(NULL);
520
	}
521
24
}
522
523
int
524
main_imsg_compose_frontend(int type, pid_t pid, void *data, uint16_t datalen)
525
{
526
24
	if (iev_frontend)
527
12
		return (imsg_compose_event(iev_frontend, type, 0, pid, -1, data,
528
		    datalen));
529
	else
530
		return (-1);
531
12
}
532
533
int
534
main_imsg_compose_engine(int type, pid_t pid, void *data, uint16_t datalen)
535
{
536
48
	if (iev_engine)
537
24
		return(imsg_compose_event(iev_engine, type, 0, pid, -1, data,
538
		    datalen));
539
	else
540
		return (-1);
541
24
}
542
543
void
544
imsg_event_add(struct imsgev *iev)
545
{
546
180
	iev->events = EV_READ;
547
90
	if (iev->ibuf.w.queued)
548
36
		iev->events |= EV_WRITE;
549
550
90
	event_del(&iev->ev);
551
90
	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
552
90
	event_add(&iev->ev, NULL);
553
90
}
554
555
int
556
imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
557
    pid_t pid, int fd, void *data, uint16_t datalen)
558
{
559
	int	ret;
560
561
108
	if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
562
36
	    datalen)) != -1)
563
36
		imsg_event_add(iev);
564
565
36
	return (ret);
566
}
567
568
static int
569
main_imsg_send_ipc_sockets(struct imsgbuf *frontend_buf,
570
    struct imsgbuf *engine_buf)
571
{
572
24
	int pipe_frontend2engine[2];
573
574
24
	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
575
24
	    PF_UNSPEC, pipe_frontend2engine) == -1)
576
		return (-1);
577
578
24
	if (imsg_compose(frontend_buf, IMSG_SOCKET_IPC, 0, 0,
579
24
	    pipe_frontend2engine[0], NULL, 0) == -1)
580
		return (-1);
581
12
	imsg_flush(frontend_buf);
582
24
	if (imsg_compose(engine_buf, IMSG_SOCKET_IPC, 0, 0,
583
24
	    pipe_frontend2engine[1], NULL, 0) == -1)
584
		return (-1);
585
12
	imsg_flush(engine_buf);
586
12
	return (0);
587
12
}
588
589
#define	ROUNDUP(a)	\
590
    (((a) & (sizeof(long) - 1)) ? (1 + ((a) | (sizeof(long) - 1))) : (a))
591
592
void
593
handle_proposal(struct imsg_proposal *proposal)
594
{
595
	struct rt_msghdr		 rtm;
596
	struct sockaddr_in6		 ifa, mask;
597
	struct sockaddr_rtlabel		 rl;
598
	struct iovec			 iov[13];
599
	long				 pad = 0;
600
	int				 iovcnt = 0, padlen;
601
602
	memset(&rtm, 0, sizeof(rtm));
603
604
	rtm.rtm_version = RTM_VERSION;
605
	rtm.rtm_type = RTM_PROPOSAL;
606
	rtm.rtm_msglen = sizeof(rtm);
607
	rtm.rtm_tableid = 0; /* XXX imsg->rdomain; */
608
	rtm.rtm_index = proposal->if_index;
609
	rtm.rtm_seq = ++rtm_seq;
610
	rtm.rtm_priority = RTP_PROPOSAL_SLAAC;
611
	rtm.rtm_addrs = (proposal->rtm_addrs & (RTA_NETMASK | RTA_IFA)) |
612
	    RTA_LABEL;
613
	rtm.rtm_flags = RTF_UP;
614
615
	iov[iovcnt].iov_base = &rtm;
616
	iov[iovcnt++].iov_len = sizeof(rtm);
617
618
	if (rtm.rtm_addrs & RTA_NETMASK) {
619
		memset(&mask, 0, sizeof(mask));
620
		mask.sin6_family = AF_INET6;
621
		mask.sin6_len = sizeof(struct sockaddr_in6);
622
		mask.sin6_addr = proposal->mask;
623
624
		iov[iovcnt].iov_base = &mask;
625
		iov[iovcnt++].iov_len = sizeof(mask);
626
		rtm.rtm_msglen += sizeof(mask);
627
		padlen = ROUNDUP(sizeof(mask)) - sizeof(mask);
628
		if (padlen > 0) {
629
			iov[iovcnt].iov_base = &pad;
630
			iov[iovcnt++].iov_len = padlen;
631
			rtm.rtm_msglen += padlen;
632
		}
633
	}
634
635
	if (rtm.rtm_addrs & RTA_IFA) {
636
		memcpy(&ifa, &proposal->addr, sizeof(ifa));
637
638
		if (ifa.sin6_family != AF_INET6 || ifa.sin6_len !=
639
		    sizeof(struct sockaddr_in6)) {
640
			log_warnx("%s: invalid address", __func__);
641
			return;
642
		}
643
644
		iov[iovcnt].iov_base = &ifa;
645
		iov[iovcnt++].iov_len = sizeof(ifa);
646
		rtm.rtm_msglen += sizeof(ifa);
647
		padlen = ROUNDUP(sizeof(ifa)) - sizeof(ifa);
648
		if (padlen > 0) {
649
			iov[iovcnt].iov_base = &pad;
650
			iov[iovcnt++].iov_len = padlen;
651
			rtm.rtm_msglen += padlen;
652
		}
653
	}
654
655
	rl.sr_len = sizeof(rl);
656
	rl.sr_family = AF_UNSPEC;
657
	if (snprintf(rl.sr_label, sizeof(rl.sr_label), "%s: %lld %d", "slaacd",
658
	    proposal->id, (int32_t)proposal->pid) >=
659
	    (ssize_t)sizeof(rl.sr_label))
660
		log_warnx("route label truncated");
661
662
	iov[iovcnt].iov_base = &rl;
663
	iov[iovcnt++].iov_len = sizeof(rl);
664
	rtm.rtm_msglen += sizeof(rl);
665
	padlen = ROUNDUP(sizeof(rl)) - sizeof(rl);
666
	if (padlen > 0) {
667
		iov[iovcnt].iov_base = &pad;
668
		iov[iovcnt++].iov_len = padlen;
669
		rtm.rtm_msglen += padlen;
670
	}
671
672
	if (writev(routesock, iov, iovcnt) == -1)
673
		log_warn("failed to send proposal");
674
}
675
676
void
677
configure_interface(struct imsg_configure_address *address)
678
{
679
680
24
	struct in6_aliasreq	 in6_addreq;
681
	time_t			 t;
682
	char			*if_name;
683
684
12
	memset(&in6_addreq, 0, sizeof(in6_addreq));
685
686
12
	if_name = if_indextoname(address->if_index, in6_addreq.ifra_name);
687
12
	if (if_name == NULL) {
688
		log_warnx("%s: cannot find interface %d", __func__,
689
		    address->if_index);
690
		return;
691
	}
692
693
12
	memcpy(&in6_addreq.ifra_addr, &address->addr,
694
	    sizeof(in6_addreq.ifra_addr));
695
12
	memcpy(&in6_addreq.ifra_prefixmask.sin6_addr, &address->mask,
696
	    sizeof(in6_addreq.ifra_prefixmask.sin6_addr));
697
12
	in6_addreq.ifra_prefixmask.sin6_family = AF_INET6;
698
12
	in6_addreq.ifra_prefixmask.sin6_len =
699
	    sizeof(in6_addreq.ifra_prefixmask);
700
701
12
	t = time(NULL);
702
703
12
	in6_addreq.ifra_lifetime.ia6t_expire = t + address->vltime;
704
12
	in6_addreq.ifra_lifetime.ia6t_vltime = address->vltime;
705
706
12
	in6_addreq.ifra_lifetime.ia6t_preferred = t + address->pltime;
707
12
	in6_addreq.ifra_lifetime.ia6t_pltime = address->pltime;
708
709
12
	in6_addreq.ifra_flags |= IN6_IFF_AUTOCONF;
710
711
12
	if (address->privacy)
712
6
		in6_addreq.ifra_flags |= IN6_IFF_PRIVACY;
713
714
12
	log_debug("%s: %s", __func__, if_name);
715
716
12
	if (ioctl(ioctl_sock, SIOCAIFADDR_IN6, &in6_addreq) < 0)
717
		fatal("SIOCAIFADDR_IN6");
718
24
}
719
720
void
721
configure_gateway(struct imsg_configure_dfr *dfr, uint8_t rtm_type)
722
{
723
12
	struct rt_msghdr		 rtm;
724
6
	struct sockaddr_rtlabel		 rl;
725
6
	struct sockaddr_in6		 dst, gw, mask;
726
6
	struct iovec			 iov[10];
727
6
	long				 pad = 0;
728
	int				 iovcnt = 0, padlen;
729
730
6
	memset(&rtm, 0, sizeof(rtm));
731
732
6
	rtm.rtm_version = RTM_VERSION;
733
6
	rtm.rtm_type = rtm_type;
734
6
	rtm.rtm_msglen = sizeof(rtm);
735
6
	rtm.rtm_tableid = 0; /* XXX imsg->rdomain; */
736
6
	rtm.rtm_index = dfr->if_index;
737
6
	rtm.rtm_seq = ++rtm_seq;
738
6
	rtm.rtm_priority = RTP_DEFAULT;
739
6
	rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_LABEL;
740
6
	rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC | RTF_MPATH;
741
742
6
	iov[iovcnt].iov_base = &rtm;
743
6
	iov[iovcnt++].iov_len = sizeof(rtm);
744
745
6
	memset(&dst, 0, sizeof(mask));
746
6
	dst.sin6_family = AF_INET6;
747
6
	dst.sin6_len = sizeof(struct sockaddr_in6);
748
749
6
	iov[iovcnt].iov_base = &dst;
750
6
	iov[iovcnt++].iov_len = sizeof(dst);
751
6
	rtm.rtm_msglen += sizeof(dst);
752
	padlen = ROUNDUP(sizeof(dst)) - sizeof(dst);
753
6
	if (padlen > 0) {
754
6
		iov[iovcnt].iov_base = &pad;
755
6
		iov[iovcnt++].iov_len = padlen;
756
6
		rtm.rtm_msglen += padlen;
757
6
	}
758
759
6
	memcpy(&gw, &dfr->addr, sizeof(gw));
760
6
	*(u_int16_t *)& gw.sin6_addr.s6_addr[2] = htons(gw.sin6_scope_id);
761
	/* gw.sin6_scope_id = 0; XXX route(8) does this*/
762
6
	iov[iovcnt].iov_base = &gw;
763
6
	iov[iovcnt++].iov_len = sizeof(gw);
764
6
	rtm.rtm_msglen += sizeof(gw);
765
	padlen = ROUNDUP(sizeof(gw)) - sizeof(gw);
766
6
	if (padlen > 0) {
767
6
		iov[iovcnt].iov_base = &pad;
768
6
		iov[iovcnt++].iov_len = padlen;
769
6
		rtm.rtm_msglen += padlen;
770
6
	}
771
772
6
	memset(&mask, 0, sizeof(mask));
773
6
	mask.sin6_family = AF_INET6;
774
6
	mask.sin6_len = sizeof(struct sockaddr_in6);
775
6
	iov[iovcnt].iov_base = &mask;
776
6
	iov[iovcnt++].iov_len = sizeof(mask);
777
6
	rtm.rtm_msglen += sizeof(mask);
778
	padlen = ROUNDUP(sizeof(mask)) - sizeof(mask);
779
6
	if (padlen > 0) {
780
6
		iov[iovcnt].iov_base = &pad;
781
6
		iov[iovcnt++].iov_len = padlen;
782
6
		rtm.rtm_msglen += padlen;
783
6
	}
784
785
6
	memset(&rl, 0, sizeof(rl));
786
6
	rl.sr_len = sizeof(rl);
787
6
	rl.sr_family = AF_UNSPEC;
788
6
	(void)snprintf(rl.sr_label, sizeof(rl.sr_label), "%s", "slaacd");
789
6
	iov[iovcnt].iov_base = &rl;
790
6
	iov[iovcnt++].iov_len = sizeof(rl);
791
6
	rtm.rtm_msglen += sizeof(rl);
792
	padlen = ROUNDUP(sizeof(rl)) - sizeof(rl);
793
6
	if (padlen > 0) {
794
6
		iov[iovcnt].iov_base = &pad;
795
6
		iov[iovcnt++].iov_len = padlen;
796
6
		rtm.rtm_msglen += padlen;
797
6
	}
798
799
6
	if (writev(routesock, iov, iovcnt) == -1)
800
6
		log_warn("failed to send route message");
801
6
}
802
803
void
804
add_gateway(struct imsg_configure_dfr *dfr)
805
{
806
12
	configure_gateway(dfr, RTM_ADD);
807
6
}
808
809
void
810
delete_gateway(struct imsg_configure_dfr *dfr)
811
{
812
	configure_gateway(dfr, RTM_DELETE);
813
}
814
815
#ifndef	SMALL
816
const char*
817
sin6_to_str(struct sockaddr_in6 *sin6)
818
{
819
	static char hbuf[NI_MAXHOST];
820
	int error;
821
822
	error = getnameinfo((struct sockaddr *)sin6, sin6->sin6_len, hbuf,
823
	    sizeof(hbuf), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV);
824
	if (error) {
825
		log_warnx("%s", gai_strerror(error));
826
		strlcpy(hbuf, "unknown", sizeof(hbuf));
827
	}
828
	return hbuf;
829
}
830
#endif	/* SMALL */