GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ospfd/ospfd.c Lines: 145 434 33.4 %
Date: 2017-11-13 Branches: 61 327 18.7 %

Line Branch Exec Source
1
/*	$OpenBSD: ospfd.c,v 1.94 2017/01/24 04:24:25 benno Exp $ */
2
3
/*
4
 * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
5
 * Copyright (c) 2004 Esben Norby <norby@openbsd.org>
6
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7
 *
8
 * Permission to use, copy, modify, and distribute this software for any
9
 * purpose with or without fee is hereby granted, provided that the above
10
 * copyright notice and this permission notice appear in all copies.
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
 */
20
21
#include <sys/types.h>
22
#include <sys/socket.h>
23
#include <sys/queue.h>
24
#include <sys/time.h>
25
#include <sys/stat.h>
26
#include <sys/wait.h>
27
#include <sys/sysctl.h>
28
#include <syslog.h>
29
30
#include <netinet/in.h>
31
#include <arpa/inet.h>
32
33
#include <event.h>
34
#include <err.h>
35
#include <errno.h>
36
#include <pwd.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <signal.h>
41
#include <unistd.h>
42
43
#include "ospfd.h"
44
#include "ospf.h"
45
#include "ospfe.h"
46
#include "control.h"
47
#include "log.h"
48
#include "rde.h"
49
50
void		main_sig_handler(int, short, void *);
51
__dead void	usage(void);
52
__dead void	ospfd_shutdown(void);
53
54
void	main_dispatch_ospfe(int, short, void *);
55
void	main_dispatch_rde(int, short, void *);
56
57
int	ospf_reload(void);
58
int	ospf_sendboth(enum imsg_type, void *, u_int16_t);
59
int	merge_interfaces(struct area *, struct area *);
60
struct iface *iface_lookup(struct area *, struct iface *);
61
62
int	pipe_parent2ospfe[2];
63
int	pipe_parent2rde[2];
64
int	pipe_ospfe2rde[2];
65
66
struct ospfd_conf	*ospfd_conf = NULL;
67
struct imsgev		*iev_ospfe;
68
struct imsgev		*iev_rde;
69
char			*conffile;
70
71
pid_t			 ospfe_pid = 0;
72
pid_t			 rde_pid = 0;
73
74
/* ARGSUSED */
75
void
76
main_sig_handler(int sig, short event, void *arg)
77
{
78
	/* signal handler rules don't apply, libevent decouples for us */
79

48
	switch (sig) {
80
	case SIGTERM:
81
	case SIGINT:
82
		ospfd_shutdown();
83
		/* NOTREACHED */
84
	case SIGHUP:
85
		if (ospf_reload() == -1)
86
			log_warnx("configuration reload failed");
87
		else
88
			log_debug("configuration reloaded");
89
		break;
90
	default:
91
		fatalx("unexpected signal");
92
		/* NOTREACHED */
93
	}
94
}
95
96
__dead void
97
usage(void)
98
{
99
	extern char *__progname;
100
101
	fprintf(stderr, "usage: %s [-cdnv] [-D macro=value]"
102
	    " [-f file] [-s socket]\n",
103
	    __progname);
104
	exit(1);
105
}
106
107
int
108
main(int argc, char *argv[])
109
{
110
48
	struct event		 ev_sigint, ev_sigterm, ev_sighup;
111
	struct area		*a;
112
	int			 ch, opts = 0;
113
	int			 debug = 0;
114
24
	int			 ipforwarding;
115
24
	int			 mib[4];
116
24
	size_t			 len;
117
	char			*sockname;
118
119
24
	conffile = CONF_FILE;
120
24
	ospfd_process = PROC_MAIN;
121
	sockname = OSPFD_SOCKET;
122
123
24
	log_init(1, LOG_DAEMON);	/* log to stderr until daemonized */
124
24
	log_procinit(log_procnames[ospfd_process]);
125
24
	log_setverbose(1);
126
127
192
	while ((ch = getopt(argc, argv, "cdD:f:ns:v")) != -1) {
128


72
		switch (ch) {
129
		case 'c':
130
			opts |= OSPFD_OPT_FORCE_DEMOTE;
131
			break;
132
		case 'd':
133
			debug = 1;
134
24
			break;
135
		case 'D':
136
			if (cmdline_symset(optarg) < 0)
137
				log_warnx("could not parse macro definition %s",
138
				    optarg);
139
			break;
140
		case 'f':
141
24
			conffile = optarg;
142
24
			break;
143
		case 'n':
144
			opts |= OSPFD_OPT_NOACTION;
145
			break;
146
		case 's':
147
			sockname = optarg;
148
			break;
149
		case 'v':
150
24
			if (opts & OSPFD_OPT_VERBOSE)
151
				opts |= OSPFD_OPT_VERBOSE2;
152
24
			opts |= OSPFD_OPT_VERBOSE;
153
24
			break;
154
		default:
155
			usage();
156
			/* NOTREACHED */
157
		}
158
	}
159
160
24
	argc -= optind;
161
24
	argv += optind;
162
24
	if (argc > 0)
163
		usage();
164
165
24
	mib[0] = CTL_NET;
166
24
	mib[1] = PF_INET;
167
24
	mib[2] = IPPROTO_IP;
168
24
	mib[3] = IPCTL_FORWARDING;
169
24
	len = sizeof(ipforwarding);
170
24
	if (sysctl(mib, 4, &ipforwarding, &len, NULL, 0) == -1)
171
		err(1, "sysctl");
172
173
24
	if (ipforwarding != 1) {
174
24
		log_warnx("WARNING: IP forwarding NOT enabled, "
175
		    "running as stub router");
176
24
		opts |= OSPFD_OPT_STUB_ROUTER;
177
24
	}
178
179
	/* fetch interfaces early */
180
24
	kif_init();
181
182
	/* parse config file */
183
24
	if ((ospfd_conf = parse_config(conffile, opts)) == NULL) {
184
		kif_clear();
185
		exit(1);
186
	}
187
24
	ospfd_conf->csock = sockname;
188
189
24
	if (ospfd_conf->opts & OSPFD_OPT_NOACTION) {
190
		if (ospfd_conf->opts & OSPFD_OPT_VERBOSE)
191
			print_config(ospfd_conf);
192
		else
193
			fprintf(stderr, "configuration OK\n");
194
		kif_clear();
195
		exit(0);
196
	}
197
198
	/* check for root privileges  */
199
24
	if (geteuid())
200
		errx(1, "need root privileges");
201
202
	/* check for ospfd user */
203
24
	if (getpwnam(OSPFD_USER) == NULL)
204
		errx(1, "unknown user %s", OSPFD_USER);
205
206
24
	log_init(debug, LOG_DAEMON);
207
24
	log_setverbose(ospfd_conf->opts & OSPFD_OPT_VERBOSE);
208
209
24
	if (!debug)
210
		daemon(1, 0);
211
212
24
	log_info("startup");
213
214
48
	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
215
24
	    PF_UNSPEC, pipe_parent2ospfe) == -1)
216
		fatal("socketpair");
217
48
	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
218
24
	    PF_UNSPEC, pipe_parent2rde) == -1)
219
		fatal("socketpair");
220
48
	if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
221
24
	    PF_UNSPEC, pipe_ospfe2rde) == -1)
222
		fatal("socketpair");
223
224
	/* start children */
225
24
	rde_pid = rde(ospfd_conf, pipe_parent2rde, pipe_ospfe2rde,
226
	    pipe_parent2ospfe);
227
24
	ospfe_pid = ospfe(ospfd_conf, pipe_parent2ospfe, pipe_ospfe2rde,
228
	    pipe_parent2rde);
229
230
24
	event_init();
231
232
	/* setup signal handler */
233
24
	signal_set(&ev_sigint, SIGINT, main_sig_handler, NULL);
234
24
	signal_set(&ev_sigterm, SIGTERM, main_sig_handler, NULL);
235
24
	signal_set(&ev_sighup, SIGHUP, main_sig_handler, NULL);
236
24
	signal_add(&ev_sigint, NULL);
237
24
	signal_add(&ev_sigterm, NULL);
238
24
	signal_add(&ev_sighup, NULL);
239
24
	signal(SIGPIPE, SIG_IGN);
240
241
	/* setup pipes to children */
242
24
	close(pipe_parent2ospfe[1]);
243
24
	close(pipe_parent2rde[1]);
244
24
	close(pipe_ospfe2rde[0]);
245
24
	close(pipe_ospfe2rde[1]);
246
247

48
	if ((iev_ospfe = malloc(sizeof(struct imsgev))) == NULL ||
248
24
	    (iev_rde = malloc(sizeof(struct imsgev))) == NULL)
249
		fatal(NULL);
250
24
	imsg_init(&iev_ospfe->ibuf, pipe_parent2ospfe[0]);
251
24
	iev_ospfe->handler = main_dispatch_ospfe;
252
24
	imsg_init(&iev_rde->ibuf, pipe_parent2rde[0]);
253
24
	iev_rde->handler = main_dispatch_rde;
254
255
	/* setup event handler */
256
24
	iev_ospfe->events = EV_READ;
257
48
	event_set(&iev_ospfe->ev, iev_ospfe->ibuf.fd, iev_ospfe->events,
258
24
	    iev_ospfe->handler, iev_ospfe);
259
24
	event_add(&iev_ospfe->ev, NULL);
260
261
24
	iev_rde->events = EV_READ;
262
48
	event_set(&iev_rde->ev, iev_rde->ibuf.fd, iev_rde->events,
263
24
	    iev_rde->handler, iev_rde);
264
24
	event_add(&iev_rde->ev, NULL);
265
266
72
	if (kr_init(!(ospfd_conf->flags & OSPFD_FLAG_NO_FIB_UPDATE),
267
48
	    ospfd_conf->rdomain) == -1)
268
		fatalx("kr_init failed");
269
270
	/* remove unneeded stuff from config */
271
96
	while ((a = LIST_FIRST(&ospfd_conf->area_list)) != NULL) {
272
48
		LIST_REMOVE(a, entry);
273
24
		area_del(a);
274
	}
275
276
	event_dispatch();
277
278
	ospfd_shutdown();
279
	/* NOTREACHED */
280
	return (0);
281
}
282
283
__dead void
284
ospfd_shutdown(void)
285
{
286
	pid_t			 pid;
287
48
	int			 status;
288
	struct redistribute	*r;
289
290
	/* close pipes */
291
24
	msgbuf_clear(&iev_ospfe->ibuf.w);
292
24
	close(iev_ospfe->ibuf.fd);
293
24
	msgbuf_clear(&iev_rde->ibuf.w);
294
24
	close(iev_rde->ibuf.fd);
295
296
24
	control_cleanup(ospfd_conf->csock);
297
48
	while ((r = SIMPLEQ_FIRST(&ospfd_conf->redist_list)) != NULL) {
298
		SIMPLEQ_REMOVE_HEAD(&ospfd_conf->redist_list, entry);
299
		free(r);
300
	}
301
24
	kr_shutdown();
302
24
	carp_demote_shutdown();
303
304
24
	log_debug("waiting for children to terminate");
305
24
	do {
306
72
		pid = wait(&status);
307
72
		if (pid == -1) {
308

48
			if (errno != EINTR && errno != ECHILD)
309
				fatal("wait");
310

96
		} else if (WIFSIGNALED(status))
311
			log_warnx("%s terminated; signal %d",
312
			    (pid == rde_pid) ? "route decision engine" :
313
			    "ospf engine", WTERMSIG(status));
314

120
	} while (pid != -1 || (pid == -1 && errno == EINTR));
315
316
	free(iev_ospfe);
317
	free(iev_rde);
318
	free(ospfd_conf);
319
320
	log_info("terminating");
321
	exit(0);
322
}
323
324
/* imsg handling */
325
/* ARGSUSED */
326
void
327
main_dispatch_ospfe(int fd, short event, void *bula)
328
{
329
48
	struct imsgev		*iev = bula;
330
	struct imsgbuf		*ibuf;
331
24
	struct imsg		 imsg;
332
24
	struct demote_msg	 dmsg;
333
	ssize_t			 n;
334
	int			 shut = 0, verbose;
335
336
24
	ibuf = &iev->ibuf;
337
338
24
	if (event & EV_READ) {
339
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
340
			fatal("imsg_read error");
341
		if (n == 0)	/* connection closed */
342
			shut = 1;
343
	}
344
24
	if (event & EV_WRITE) {
345

24
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
346
			fatal("msgbuf_write");
347
24
		if (n == 0)	/* connection closed */
348
			shut = 1;
349
	}
350
351
24
	for (;;) {
352
24
		if ((n = imsg_get(ibuf, &imsg)) == -1)
353
			fatal("imsg_get");
354
355
24
		if (n == 0)
356
			break;
357
358
		switch (imsg.hdr.type) {
359
		case IMSG_CTL_RELOAD:
360
			if (ospf_reload() == -1)
361
				log_warnx("configuration reload failed");
362
			else
363
				log_debug("configuration reloaded");
364
			break;
365
		case IMSG_CTL_FIB_COUPLE:
366
			kr_fib_couple();
367
			break;
368
		case IMSG_CTL_FIB_DECOUPLE:
369
			kr_fib_decouple();
370
			break;
371
		case IMSG_CTL_FIB_RELOAD:
372
			kr_fib_reload();
373
			break;
374
		case IMSG_CTL_KROUTE:
375
		case IMSG_CTL_KROUTE_ADDR:
376
			kr_show_route(&imsg);
377
			break;
378
		case IMSG_CTL_IFINFO:
379
			if (imsg.hdr.len == IMSG_HEADER_SIZE)
380
				kr_ifinfo(NULL, imsg.hdr.pid);
381
			else if (imsg.hdr.len == IMSG_HEADER_SIZE + IFNAMSIZ)
382
				kr_ifinfo(imsg.data, imsg.hdr.pid);
383
			else
384
				log_warnx("IFINFO request with wrong len");
385
			break;
386
		case IMSG_DEMOTE:
387
			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(dmsg))
388
				fatalx("invalid size of OE request");
389
			memcpy(&dmsg, imsg.data, sizeof(dmsg));
390
			carp_demote_set(dmsg.demote_group, dmsg.level);
391
			break;
392
		case IMSG_CTL_LOG_VERBOSE:
393
			/* already checked by ospfe */
394
			memcpy(&verbose, imsg.data, sizeof(verbose));
395
			log_setverbose(verbose);
396
			break;
397
		default:
398
			log_debug("main_dispatch_ospfe: error handling imsg %d",
399
			    imsg.hdr.type);
400
			break;
401
		}
402
		imsg_free(&imsg);
403
	}
404
24
	if (!shut)
405
24
		imsg_event_add(iev);
406
	else {
407
		/* this pipe is dead, so remove the event handler */
408
		event_del(&iev->ev);
409
		event_loopexit(NULL);
410
	}
411
24
}
412
413
/* ARGSUSED */
414
void
415
main_dispatch_rde(int fd, short event, void *bula)
416
{
417
48
	struct imsgev	*iev = bula;
418
	struct imsgbuf  *ibuf;
419
24
	struct imsg	 imsg;
420
	ssize_t		 n;
421
	int		 count, shut = 0;
422
423
24
	ibuf = &iev->ibuf;
424
425
24
	if (event & EV_READ) {
426

24
		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
427
			fatal("imsg_read error");
428
24
		if (n == 0)	/* connection closed */
429
			shut = 1;
430
	}
431
24
	if (event & EV_WRITE) {
432
		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
433
			fatal("msgbuf_write");
434
		if (n == 0)	/* connection closed */
435
			shut = 1;
436
	}
437
438
48
	for (;;) {
439
48
		if ((n = imsg_get(ibuf, &imsg)) == -1)
440
			fatal("imsg_get");
441
442
48
		if (n == 0)
443
			break;
444
445
24
		switch (imsg.hdr.type) {
446
		case IMSG_KROUTE_CHANGE:
447
			count = (imsg.hdr.len - IMSG_HEADER_SIZE) /
448
			    sizeof(struct kroute);
449
			if (kr_change(imsg.data, count))
450
				log_warn("main_dispatch_rde: error changing "
451
				    "route");
452
			break;
453
		case IMSG_KROUTE_DELETE:
454
24
			if (kr_delete(imsg.data))
455
				log_warn("main_dispatch_rde: error deleting "
456
				    "route");
457
			break;
458
		default:
459
			log_debug("main_dispatch_rde: error handling imsg %d",
460
			    imsg.hdr.type);
461
			break;
462
		}
463
24
		imsg_free(&imsg);
464
	}
465
24
	if (!shut)
466
24
		imsg_event_add(iev);
467
	else {
468
		/* this pipe is dead, so remove the event handler */
469
		event_del(&iev->ev);
470
		event_loopexit(NULL);
471
	}
472
24
}
473
474
void
475
main_imsg_compose_ospfe(int type, pid_t pid, void *data, u_int16_t datalen)
476
{
477
384
	if (iev_ospfe)
478
24
		imsg_compose_event(iev_ospfe, type, 0, pid, -1, data, datalen);
479
192
}
480
481
void
482
main_imsg_compose_rde(int type, pid_t pid, void *data, u_int16_t datalen)
483
{
484
	if (iev_rde)
485
		imsg_compose_event(iev_rde, type, 0, pid, -1, data, datalen);
486
}
487
488
void
489
imsg_event_add(struct imsgev *iev)
490
{
491
144
	iev->events = EV_READ;
492
72
	if (iev->ibuf.w.queued)
493
24
		iev->events |= EV_WRITE;
494
495
72
	event_del(&iev->ev);
496
72
	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev);
497
72
	event_add(&iev->ev, NULL);
498
72
}
499
500
int
501
imsg_compose_event(struct imsgev *iev, u_int16_t type, u_int32_t peerid,
502
    pid_t pid, int fd, void *data, u_int16_t datalen)
503
{
504
	int	ret;
505
506
72
	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
507
24
	    pid, fd, data, datalen)) != -1)
508
24
		imsg_event_add(iev);
509
24
	return (ret);
510
}
511
512
int
513
ospf_redistribute(struct kroute *kr, u_int32_t *metric)
514
{
515
	struct redistribute	*r;
516
	u_int8_t		 is_default = 0;
517
518
	/* only allow 0.0.0.0/0 via REDIST_DEFAULT */
519

288
	if (kr->prefix.s_addr == INADDR_ANY && kr->prefixlen == 0)
520
48
		is_default = 1;
521
522
240
	SIMPLEQ_FOREACH(r, &ospfd_conf->redist_list, entry) {
523
		switch (r->type & ~REDIST_NO) {
524
		case REDIST_LABEL:
525
			if (kr->rtlabel == r->label) {
526
				*metric = r->metric;
527
				return (r->type & REDIST_NO ? 0 : 1);
528
			}
529
			break;
530
		case REDIST_STATIC:
531
			/*
532
			 * Dynamic routes are not redistributable. Placed here
533
			 * so that link local addresses can be redistributed
534
			 * via a rtlabel.
535
			 */
536
			if (is_default)
537
				continue;
538
			if (kr->flags & F_DYNAMIC)
539
				continue;
540
			if (kr->flags & F_STATIC) {
541
				*metric = r->metric;
542
				return (r->type & REDIST_NO ? 0 : 1);
543
			}
544
			break;
545
		case REDIST_CONNECTED:
546
			if (is_default)
547
				continue;
548
			if (kr->flags & F_DYNAMIC)
549
				continue;
550
			if (kr->flags & F_CONNECTED) {
551
				*metric = r->metric;
552
				return (r->type & REDIST_NO ? 0 : 1);
553
			}
554
			break;
555
		case REDIST_ADDR:
556
			if (kr->flags & F_DYNAMIC)
557
				continue;
558
559
			if (r->addr.s_addr == INADDR_ANY &&
560
			    r->mask.s_addr == INADDR_ANY) {
561
				if (is_default) {
562
					*metric = r->metric;
563
					return (r->type & REDIST_NO ? 0 : 1);
564
				} else
565
					return (0);
566
			}
567
568
			if ((kr->prefix.s_addr & r->mask.s_addr) ==
569
			    (r->addr.s_addr & r->mask.s_addr) &&
570
			    kr->prefixlen >= mask2prefixlen(r->mask.s_addr)) {
571
				*metric = r->metric;
572
				return (r->type & REDIST_NO ? 0 : 1);
573
			}
574
			break;
575
		case REDIST_DEFAULT:
576
			if (is_default) {
577
				*metric = r->metric;
578
				return (r->type & REDIST_NO ? 0 : 1);
579
			}
580
			break;
581
		}
582
	}
583
584
120
	return (0);
585
120
}
586
587
int
588
ospf_reload(void)
589
{
590
	struct area		*area;
591
	struct iface		*iface;
592
	struct ospfd_conf	*xconf;
593
	struct redistribute	*r;
594
595
	if ((xconf = parse_config(conffile, ospfd_conf->opts)) == NULL)
596
		return (-1);
597
598
	/* send config to childs */
599
	if (ospf_sendboth(IMSG_RECONF_CONF, xconf, sizeof(*xconf)) == -1)
600
		return (-1);
601
602
	/* send interfaces */
603
	LIST_FOREACH(area, &xconf->area_list, entry) {
604
		if (ospf_sendboth(IMSG_RECONF_AREA, area, sizeof(*area)) == -1)
605
			return (-1);
606
607
		SIMPLEQ_FOREACH(r, &area->redist_list, entry) {
608
			main_imsg_compose_rde(IMSG_RECONF_REDIST, 0, r,
609
			    sizeof(*r));
610
		}
611
		LIST_FOREACH(iface, &area->iface_list, entry) {
612
			if (ospf_sendboth(IMSG_RECONF_IFACE, iface,
613
			    sizeof(*iface)) == -1)
614
				return (-1);
615
			if (iface->auth_type == AUTH_CRYPT)
616
				if (md_list_send(&iface->auth_md_list,
617
				    iev_ospfe) == -1)
618
					return (-1);
619
		}
620
	}
621
622
	if (ospf_sendboth(IMSG_RECONF_END, NULL, 0) == -1)
623
		return (-1);
624
625
	merge_config(ospfd_conf, xconf);
626
	/* update redistribute lists */
627
	kr_reload();
628
	return (0);
629
}
630
631
int
632
ospf_sendboth(enum imsg_type type, void *buf, u_int16_t len)
633
{
634
	if (imsg_compose_event(iev_ospfe, type, 0, 0, -1, buf, len) == -1)
635
		return (-1);
636
	if (imsg_compose_event(iev_rde, type, 0, 0, -1, buf, len) == -1)
637
		return (-1);
638
	return (0);
639
}
640
641
void
642
merge_config(struct ospfd_conf *conf, struct ospfd_conf *xconf)
643
{
644
	struct area		*a, *xa, *na;
645
	struct iface		*iface;
646
	struct redistribute	*r;
647
	int			 rchange = 0;
648
649
	/* change of rtr_id needs a restart */
650
	conf->flags = xconf->flags;
651
	conf->spf_delay = xconf->spf_delay;
652
	conf->spf_hold_time = xconf->spf_hold_time;
653
	if (SIMPLEQ_EMPTY(&conf->redist_list) !=
654
	    SIMPLEQ_EMPTY(&xconf->redist_list))
655
		rchange = 1;
656
	conf->rfc1583compat = xconf->rfc1583compat;
657
658
	if (ospfd_process == PROC_MAIN) {
659
		/* main process does neither use areas nor interfaces */
660
		while ((r = SIMPLEQ_FIRST(&conf->redist_list)) != NULL) {
661
			SIMPLEQ_REMOVE_HEAD(&conf->redist_list, entry);
662
			free(r);
663
		}
664
		while ((r = SIMPLEQ_FIRST(&xconf->redist_list)) != NULL) {
665
			SIMPLEQ_REMOVE_HEAD(&xconf->redist_list, entry);
666
			SIMPLEQ_INSERT_TAIL(&conf->redist_list, r, entry);
667
		}
668
		goto done;
669
	}
670
671
	/* merge areas and interfaces */
672
	for (a = LIST_FIRST(&conf->area_list); a != NULL; a = na) {
673
		na = LIST_NEXT(a, entry);
674
		/* find deleted areas */
675
		if ((xa = area_find(xconf, a->id)) == NULL) {
676
			if (ospfd_process == PROC_OSPF_ENGINE) {
677
				LIST_FOREACH(iface, &a->iface_list, entry)
678
					if_fsm(iface, IF_EVT_DOWN);
679
			}
680
			LIST_REMOVE(a, entry);
681
			area_del(a);
682
		}
683
	}
684
685
	for (xa = LIST_FIRST(&xconf->area_list); xa != NULL; xa = na) {
686
		na = LIST_NEXT(xa, entry);
687
		if ((a = area_find(conf, xa->id)) == NULL) {
688
			LIST_REMOVE(xa, entry);
689
			LIST_INSERT_HEAD(&conf->area_list, xa, entry);
690
			if (ospfd_process == PROC_OSPF_ENGINE) {
691
				/* start interfaces */
692
				ospfe_demote_area(xa, 0);
693
				LIST_FOREACH(iface, &xa->iface_list, entry) {
694
					if_init(conf, iface);
695
					if (if_fsm(iface, IF_EVT_UP)) {
696
						log_debug("error starting "
697
						    "interface %s",
698
						    iface->name);
699
					}
700
				}
701
			}
702
			/* no need to merge interfaces */
703
			continue;
704
		}
705
		/*
706
		 * stub is not yet used but switching between stub and normal
707
		 * will be another painful job.
708
		 */
709
		if (a->stub != xa->stub && ospfd_process == PROC_OSPF_ENGINE)
710
			a->dirty = 1; /* force rtr LSA update */
711
		if (xa->stub && ospfd_process == PROC_RDE_ENGINE) {
712
			while ((r = SIMPLEQ_FIRST(&a->redist_list)) != NULL) {
713
				SIMPLEQ_REMOVE_HEAD(&a->redist_list, entry);
714
				free(r);
715
			}
716
717
			while ((r = SIMPLEQ_FIRST(&xa->redist_list)) != NULL) {
718
				SIMPLEQ_REMOVE_HEAD(&xa->redist_list, entry);
719
				SIMPLEQ_INSERT_TAIL(&a->redist_list, r, entry);
720
			}
721
		}
722
723
		a->stub = xa->stub;
724
		a->stub_default_cost = xa->stub_default_cost;
725
		if (ospfd_process == PROC_RDE_ENGINE)
726
			a->dirty = 1; /* force SPF tree recalculation */
727
728
		/* merge interfaces */
729
		if (merge_interfaces(a, xa) &&
730
		    ospfd_process == PROC_OSPF_ENGINE)
731
			a->dirty = 1; /* force rtr LSA update */
732
	}
733
734
	if (ospfd_process == PROC_OSPF_ENGINE) {
735
		LIST_FOREACH(a, &conf->area_list, entry) {
736
			LIST_FOREACH(iface, &a->iface_list, entry) {
737
				if (iface->state == IF_STA_NEW) {
738
					iface->state = IF_STA_DOWN;
739
					if_init(conf, iface);
740
					if (if_fsm(iface, IF_EVT_UP)) {
741
						log_debug("error starting "
742
						    "interface %s",
743
						    iface->name);
744
					}
745
				}
746
			}
747
			if (a->dirty || rchange) {
748
				a->dirty = 0;
749
				orig_rtr_lsa(a);
750
			}
751
		}
752
	}
753
	if (ospfd_process == PROC_RDE_ENGINE) {
754
		LIST_FOREACH(a, &conf->area_list, entry) {
755
			if (a->dirty) {
756
				start_spf_timer();
757
				break;
758
			}
759
		}
760
	}
761
762
done:
763
	while ((a = LIST_FIRST(&xconf->area_list)) != NULL) {
764
		LIST_REMOVE(a, entry);
765
		area_del(a);
766
	}
767
	free(xconf);
768
}
769
770
int
771
merge_interfaces(struct area *a, struct area *xa)
772
{
773
	struct iface	*i, *xi, *ni;
774
	int		 dirty = 0;
775
776
	/* problems:
777
	 * - new interfaces (easy)
778
	 * - deleted interfaces (needs to be done via fsm?)
779
	 * - changing passive (painful?)
780
	 */
781
	for (i = LIST_FIRST(&a->iface_list); i != NULL; i = ni) {
782
		ni = LIST_NEXT(i, entry);
783
		if (iface_lookup(xa, i) == NULL) {
784
			log_debug("merge_interfaces: proc %d area %s removing "
785
			    "interface %s", ospfd_process, inet_ntoa(a->id),
786
			    i->name);
787
			if (ospfd_process == PROC_OSPF_ENGINE)
788
				if_fsm(i, IF_EVT_DOWN);
789
			else if (ospfd_process == PROC_RDE_ENGINE)
790
				rde_nbr_iface_del(i);
791
			LIST_REMOVE(i, entry);
792
			if_del(i);
793
		}
794
	}
795
796
	for (xi = LIST_FIRST(&xa->iface_list); xi != NULL; xi = ni) {
797
		ni = LIST_NEXT(xi, entry);
798
		if ((i = iface_lookup(a, xi)) == NULL) {
799
			/* new interface but delay initialisation */
800
			log_debug("merge_interfaces: proc %d area %s adding "
801
			    "interface %s", ospfd_process, inet_ntoa(a->id),
802
			    xi->name);
803
			LIST_REMOVE(xi, entry);
804
			LIST_INSERT_HEAD(&a->iface_list, xi, entry);
805
			xi->area = a;
806
			if (ospfd_process == PROC_OSPF_ENGINE)
807
				xi->state = IF_STA_NEW;
808
			continue;
809
		}
810
		log_debug("merge_interfaces: proc %d area %s merging "
811
		    "interface %s", ospfd_process, inet_ntoa(a->id), i->name);
812
		i->dst = xi->dst;
813
		i->abr_id = xi->abr_id;
814
		i->baudrate = xi->baudrate;
815
		i->dead_interval = xi->dead_interval;
816
		i->mtu = xi->mtu;
817
		i->transmit_delay = xi->transmit_delay;
818
		i->hello_interval = xi->hello_interval;
819
		i->rxmt_interval = xi->rxmt_interval;
820
		if (i->metric != xi->metric)
821
			dirty = 1;
822
		i->metric = xi->metric;
823
		i->priority = xi->priority;
824
		if (i->self)
825
			i->self->priority = i->priority;
826
		i->flags = xi->flags; /* needed? */
827
		i->type = xi->type; /* needed? */
828
		i->if_type = xi->if_type; /* needed? */
829
		i->linkstate = xi->linkstate; /* needed? */
830
831
		i->auth_type = xi->auth_type;
832
		strncpy(i->auth_key, xi->auth_key, MAX_SIMPLE_AUTH_LEN);
833
		md_list_clr(&i->auth_md_list);
834
		md_list_copy(&i->auth_md_list, &xi->auth_md_list);
835
836
		if (i->passive != xi->passive) {
837
			/* need to restart interface to cope with this change */
838
			if (ospfd_process == PROC_OSPF_ENGINE)
839
				if_fsm(i, IF_EVT_DOWN);
840
			i->passive = xi->passive;
841
			if (ospfd_process == PROC_OSPF_ENGINE)
842
				if_fsm(i, IF_EVT_UP);
843
		}
844
	}
845
	return (dirty);
846
}
847
848
struct iface *
849
iface_lookup(struct area *area, struct iface *iface)
850
{
851
	struct iface	*i;
852
853
	LIST_FOREACH(i, &area->iface_list, entry)
854
		if (i->ifindex == iface->ifindex &&
855
		    i->addr.s_addr == iface->addr.s_addr &&
856
		    i->mask.s_addr == iface->mask.s_addr)
857
			return (i);
858
	return (NULL);
859
}