GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: sbin/slaacd/slaacd.c Lines: 143 403 35.5 %
Date: 2017-11-07 Branches: 58 188 30.9 %

Line Branch Exec Source
1
/*	$OpenBSD: slaacd.c,v 1.11 2017/08/23 15:49:08 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

18
	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
18
	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
9
	int		 pipe_main2frontend[2];
153
9
	int		 pipe_main2engine[2];
154
155
9
	csock = SLAACD_SOCKET;
156
157
9
	log_init(1, LOG_DAEMON);	/* Log to stderr until daemonized. */
158
9
	log_setverbose(1);
159
160
9
	saved_argv0 = argv[0];
161
9
	if (saved_argv0 == NULL)
162
		saved_argv0 = "slaacd";
163
164
27
	while ((ch = getopt(argc, argv, "dEFs:v")) != -1) {
165

9
		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
9
			csock = optarg;
177
9
			break;
178
		case 'v':
179
			verbose++;
180
			break;
181
		default:
182
			usage();
183
		}
184
	}
185
186
9
	argc -= optind;
187
9
	argv += optind;
188

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

18
	if ((iev_frontend = malloc(sizeof(struct imsgev))) == NULL ||
247
9
	    (iev_engine = malloc(sizeof(struct imsgev))) == NULL)
248
		fatal(NULL);
249
9
	imsg_init(&iev_frontend->ibuf, pipe_main2frontend[0]);
250
9
	iev_frontend->handler = main_dispatch_frontend;
251
9
	imsg_init(&iev_engine->ibuf, pipe_main2engine[0]);
252
9
	iev_engine->handler = main_dispatch_engine;
253
254
	/* Setup event handlers for pipes to engine & frontend. */
255
9
	iev_frontend->events = EV_READ;
256
18
	event_set(&iev_frontend->ev, iev_frontend->ibuf.fd,
257
9
	    iev_frontend->events, iev_frontend->handler, iev_frontend);
258
9
	event_add(&iev_frontend->ev, NULL);
259
260
9
	iev_engine->events = EV_READ;
261
18
	event_set(&iev_engine->ev, iev_engine->ibuf.fd, iev_engine->events,
262
9
	    iev_engine->handler, iev_engine);
263
9
	event_add(&iev_engine->ev, NULL);
264
265
9
	if (main_imsg_send_ipc_sockets(&iev_frontend->ibuf, &iev_engine->ibuf))
266
		fatal("could not establish imsg links");
267
268
9
	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
18
	int	 status;
290
291
	/* Close pipes. */
292
9
	msgbuf_clear(&iev_frontend->ibuf.w);
293
9
	close(iev_frontend->ibuf.fd);
294
9
	msgbuf_clear(&iev_engine->ibuf.w);
295
9
	close(iev_engine->ibuf.fd);
296
297
9
	log_debug("waiting for children to terminate");
298
9
	do {
299
27
		pid = wait(&status);
300
27
		if (pid == -1) {
301

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

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

45
	} 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
36
	char	*argv[8];
324
	int	 argc = 0;
325
	pid_t	 pid;
326
327
18
	switch (pid = fork()) {
328
	case -1:
329
		fatal("cannot fork");
330
	case 0:
331
		break;
332
	default:
333
18
		close(fd);
334
18
		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
18
}
366
367
void
368
main_dispatch_frontend(int fd, short event, void *bula)
369
{
370
36
	struct imsgev		*iev = bula;
371
	struct imsgbuf		*ibuf;
372
18
	struct imsg		 imsg;
373
18
	struct imsg_ifinfo	 imsg_ifinfo;
374
	ssize_t			 n;
375
	int			 shut = 0;
376
#ifndef	SMALL
377
18
	struct imsg_addrinfo	 imsg_addrinfo;
378
	int			 verbose;
379
#endif	/* SMALL */
380
381
18
	ibuf = &iev->ibuf;
382
383
18
	if (event & EV_READ) {
384

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

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

9
		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
9
			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
9
			memcpy(&imsg_ifinfo, imsg.data, sizeof(imsg_ifinfo));
426
9
			main_imsg_compose_engine(IMSG_UPDATE_IF, 0,
427
			    &imsg_ifinfo, sizeof(imsg_ifinfo));
428
9
			break;
429
		default:
430
			log_debug("%s: error handling imsg %d", __func__,
431
			    imsg.hdr.type);
432
			break;
433
		}
434
9
		imsg_free(&imsg);
435
	}
436
18
	if (!shut)
437
18
		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
18
}
444
445
void
446
main_dispatch_engine(int fd, short event, void *bula)
447
{
448
18
	struct imsgev			*iev = bula;
449
	struct imsgbuf			*ibuf;
450
9
	struct imsg			 imsg;
451
9
	struct imsg_proposal		 proposal;
452
9
	struct imsg_configure_address	 address;
453
9
	struct imsg_configure_dfr	 dfr;
454
	ssize_t				 n;
455
	int				 shut = 0;
456
457
9
	ibuf = &iev->ibuf;
458
459
9
	if (event & EV_READ) {
460
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
461
			fatal("imsg_read error");
462
		if (n == 0)	/* Connection closed. */
463
			shut = 1;
464
	}
465
9
	if (event & EV_WRITE) {
466

9
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
467
			fatal("msgbuf_write");
468
9
		if (n == 0)	/* Connection closed. */
469
			shut = 1;
470
	}
471
472
	for (;;) {
473
9
		if ((n = imsg_get(ibuf, &imsg)) == -1)
474
			fatal("imsg_get");
475
9
		if (n == 0)	/* No more messages. */
476
			break;
477
478
		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
			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(address))
488
				fatal("%s: IMSG_CONFIGURE_ADDRESS wrong "
489
				    "length: %d", __func__, imsg.hdr.len);
490
			memcpy(&address, imsg.data, sizeof(address));
491
			configure_interface(&address);
492
			break;
493
		case IMSG_CONFIGURE_DFR:
494
			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(dfr))
495
				fatal("%s: IMSG_CONFIGURE_DFR wrong "
496
				    "length: %d", __func__, imsg.hdr.len);
497
			memcpy(&dfr, imsg.data, sizeof(dfr));
498
			add_gateway(&dfr);
499
			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
		imsg_free(&imsg);
513
	}
514
9
	if (!shut)
515
9
		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
9
}
522
523
int
524
main_imsg_compose_frontend(int type, pid_t pid, void *data, uint16_t datalen)
525
{
526
18
	if (iev_frontend)
527
9
		return (imsg_compose_event(iev_frontend, type, 0, pid, -1, data,
528
		    datalen));
529
	else
530
		return (-1);
531
9
}
532
533
int
534
main_imsg_compose_engine(int type, pid_t pid, void *data, uint16_t datalen)
535
{
536
18
	if (iev_engine)
537
9
		return(imsg_compose_event(iev_engine, type, 0, pid, -1, data,
538
		    datalen));
539
	else
540
		return (-1);
541
9
}
542
543
void
544
imsg_event_add(struct imsgev *iev)
545
{
546
90
	iev->events = EV_READ;
547
45
	if (iev->ibuf.w.queued)
548
18
		iev->events |= EV_WRITE;
549
550
45
	event_del(&iev->ev);
551
45
	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
552
45
	event_add(&iev->ev, NULL);
553
45
}
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
54
	if ((ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data,
562
18
	    datalen)) != -1)
563
18
		imsg_event_add(iev);
564
565
18
	return (ret);
566
}
567
568
static int
569
main_imsg_send_ipc_sockets(struct imsgbuf *frontend_buf,
570
    struct imsgbuf *engine_buf)
571
{
572
18
	int pipe_frontend2engine[2];
573
574
18
	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
575
18
	    PF_UNSPEC, pipe_frontend2engine) == -1)
576
		return (-1);
577
578
18
	if (imsg_compose(frontend_buf, IMSG_SOCKET_IPC, 0, 0,
579
18
	    pipe_frontend2engine[0], NULL, 0) == -1)
580
		return (-1);
581
9
	imsg_flush(frontend_buf);
582
18
	if (imsg_compose(engine_buf, IMSG_SOCKET_IPC, 0, 0,
583
18
	    pipe_frontend2engine[1], NULL, 0) == -1)
584
		return (-1);
585
9
	imsg_flush(engine_buf);
586
9
	return (0);
587
9
}
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
	struct in6_aliasreq	 in6_addreq;
681
	time_t			 t;
682
	char			*if_name;
683
684
	memset(&in6_addreq, 0, sizeof(in6_addreq));
685
686
	if_name = if_indextoname(address->if_index, in6_addreq.ifra_name);
687
	if (if_name == NULL) {
688
		log_warnx("%s: cannot find interface %d", __func__,
689
		    address->if_index);
690
		return;
691
	}
692
693
	memcpy(&in6_addreq.ifra_addr, &address->addr,
694
	    sizeof(in6_addreq.ifra_addr));
695
	memcpy(&in6_addreq.ifra_prefixmask.sin6_addr, &address->mask,
696
	    sizeof(in6_addreq.ifra_prefixmask.sin6_addr));
697
	in6_addreq.ifra_prefixmask.sin6_family = AF_INET6;
698
	in6_addreq.ifra_prefixmask.sin6_len =
699
	    sizeof(in6_addreq.ifra_prefixmask);
700
701
	t = time(NULL);
702
703
	in6_addreq.ifra_lifetime.ia6t_expire = t + address->vltime;
704
	in6_addreq.ifra_lifetime.ia6t_vltime = address->vltime;
705
706
	in6_addreq.ifra_lifetime.ia6t_preferred = t + address->pltime;
707
	in6_addreq.ifra_lifetime.ia6t_pltime = address->pltime;
708
709
	in6_addreq.ifra_flags |= IN6_IFF_AUTOCONF;
710
711
	if (address->privacy)
712
		in6_addreq.ifra_flags |= IN6_IFF_PRIVACY;
713
714
	log_debug("%s: %s", __func__, if_name);
715
716
	if (ioctl(ioctl_sock, SIOCAIFADDR_IN6, &in6_addreq) < 0)
717
		fatal("SIOCAIFADDR_IN6");
718
}
719
720
void
721
configure_gateway(struct imsg_configure_dfr *dfr, uint8_t rtm_type)
722
{
723
	struct rt_msghdr		 rtm;
724
	struct sockaddr_rtlabel		 rl;
725
	struct sockaddr_in6		 dst, gw, mask;
726
	struct iovec			 iov[10];
727
	long				 pad = 0;
728
	int				 iovcnt = 0, padlen;
729
730
	memset(&rtm, 0, sizeof(rtm));
731
732
	rtm.rtm_version = RTM_VERSION;
733
	rtm.rtm_type = rtm_type;
734
	rtm.rtm_msglen = sizeof(rtm);
735
	rtm.rtm_tableid = 0; /* XXX imsg->rdomain; */
736
	rtm.rtm_index = dfr->if_index;
737
	rtm.rtm_seq = ++rtm_seq;
738
	rtm.rtm_priority = RTP_DEFAULT;
739
	rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_LABEL;
740
	rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
741
742
	iov[iovcnt].iov_base = &rtm;
743
	iov[iovcnt++].iov_len = sizeof(rtm);
744
745
	memset(&dst, 0, sizeof(mask));
746
	dst.sin6_family = AF_INET6;
747
	dst.sin6_len = sizeof(struct sockaddr_in6);
748
749
	iov[iovcnt].iov_base = &dst;
750
	iov[iovcnt++].iov_len = sizeof(dst);
751
	rtm.rtm_msglen += sizeof(dst);
752
	padlen = ROUNDUP(sizeof(dst)) - sizeof(dst);
753
	if (padlen > 0) {
754
		iov[iovcnt].iov_base = &pad;
755
		iov[iovcnt++].iov_len = padlen;
756
		rtm.rtm_msglen += padlen;
757
	}
758
759
	memcpy(&gw, &dfr->addr, sizeof(gw));
760
	*(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
	iov[iovcnt].iov_base = &gw;
763
	iov[iovcnt++].iov_len = sizeof(gw);
764
	rtm.rtm_msglen += sizeof(gw);
765
	padlen = ROUNDUP(sizeof(gw)) - sizeof(gw);
766
	if (padlen > 0) {
767
		iov[iovcnt].iov_base = &pad;
768
		iov[iovcnt++].iov_len = padlen;
769
		rtm.rtm_msglen += padlen;
770
	}
771
772
	memset(&mask, 0, sizeof(mask));
773
	mask.sin6_family = AF_INET6;
774
	mask.sin6_len = sizeof(struct sockaddr_in6);
775
	iov[iovcnt].iov_base = &mask;
776
	iov[iovcnt++].iov_len = sizeof(mask);
777
	rtm.rtm_msglen += sizeof(mask);
778
	padlen = ROUNDUP(sizeof(mask)) - sizeof(mask);
779
	if (padlen > 0) {
780
		iov[iovcnt].iov_base = &pad;
781
		iov[iovcnt++].iov_len = padlen;
782
		rtm.rtm_msglen += padlen;
783
	}
784
785
	memset(&rl, 0, sizeof(rl));
786
	rl.sr_len = sizeof(rl);
787
	rl.sr_family = AF_UNSPEC;
788
	(void)snprintf(rl.sr_label, sizeof(rl.sr_label), "%s", "slaacd");
789
	iov[iovcnt].iov_base = &rl;
790
	iov[iovcnt++].iov_len = sizeof(rl);
791
	rtm.rtm_msglen += sizeof(rl);
792
	padlen = ROUNDUP(sizeof(rl)) - sizeof(rl);
793
	if (padlen > 0) {
794
		iov[iovcnt].iov_base = &pad;
795
		iov[iovcnt++].iov_len = padlen;
796
		rtm.rtm_msglen += padlen;
797
	}
798
799
	if (writev(routesock, iov, iovcnt) == -1)
800
		log_warn("failed to send route message");
801
}
802
803
void
804
add_gateway(struct imsg_configure_dfr *dfr)
805
{
806
	configure_gateway(dfr, RTM_ADD);
807
}
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 */