rde.c revision 1.49
1/*	$OpenBSD: rde.c,v 1.49 2010/07/09 12:39:46 bluhm Exp $ */
2
3/*
4 * Copyright (c) 2004, 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/param.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <err.h>
28#include <errno.h>
29#include <stdlib.h>
30#include <signal.h>
31#include <string.h>
32#include <pwd.h>
33#include <unistd.h>
34#include <event.h>
35
36#include "ospf6.h"
37#include "ospf6d.h"
38#include "ospfe.h"
39#include "log.h"
40#include "rde.h"
41
42void		 rde_sig_handler(int sig, short, void *);
43void		 rde_shutdown(void);
44void		 rde_dispatch_imsg(int, short, void *);
45void		 rde_dispatch_parent(int, short, void *);
46void		 rde_dump_area(struct area *, int, pid_t);
47
48void		 rde_send_summary(pid_t);
49void		 rde_send_summary_area(struct area *, pid_t);
50void		 rde_nbr_init(u_int32_t);
51void		 rde_nbr_free(void);
52struct rde_nbr	*rde_nbr_new(u_int32_t, struct rde_nbr *);
53void		 rde_nbr_del(struct rde_nbr *);
54
55void		 rde_req_list_add(struct rde_nbr *, struct lsa_hdr *);
56int		 rde_req_list_exists(struct rde_nbr *, struct lsa_hdr *);
57void		 rde_req_list_del(struct rde_nbr *, struct lsa_hdr *);
58void		 rde_req_list_free(struct rde_nbr *);
59
60struct lsa	*rde_asext_get(struct rroute *);
61struct lsa	*rde_asext_put(struct rroute *);
62
63int		 comp_asext(struct lsa *, struct lsa *);
64struct lsa	*orig_asext_lsa(struct rroute *, u_int16_t);
65struct lsa	*orig_sum_lsa(struct rt_node *, struct area *, u_int8_t, int);
66struct lsa	*orig_intra_lsa_net(struct area *, struct iface *,
67		 struct vertex *);
68struct lsa	*orig_intra_lsa_rtr(struct area *, struct vertex *);
69void		 append_prefix_lsa(struct lsa **, u_int16_t *,
70		    struct lsa_prefix *);
71
72/* A 32-bit value != any ifindex.
73 * We assume ifindex is bound by [1, USHRT_MAX] inclusive. */
74#define	LS_ID_INTRA_RTR	0x01000000
75
76/* Tree of prefixes with global scope on given a link,
77 * see orig_intra_lsa_*() */
78struct prefix_node {
79	RB_ENTRY(prefix_node)	 entry;
80	struct lsa_prefix	*prefix;
81};
82RB_HEAD(prefix_tree, prefix_node);
83RB_PROTOTYPE(prefix_tree, prefix_node, entry, prefix_compare);
84int		 prefix_compare(struct prefix_node *, struct prefix_node *);
85void		 prefix_tree_add(struct prefix_tree *, struct lsa_link *);
86
87struct ospfd_conf	*rdeconf = NULL, *nconf = NULL;
88struct imsgev		*iev_ospfe;
89struct imsgev		*iev_main;
90struct rde_nbr		*nbrself;
91struct lsa_tree		 asext_tree;
92
93/* ARGSUSED */
94void
95rde_sig_handler(int sig, short event, void *arg)
96{
97	/*
98	 * signal handler rules don't apply, libevent decouples for us
99	 */
100
101	switch (sig) {
102	case SIGINT:
103	case SIGTERM:
104		rde_shutdown();
105		/* NOTREACHED */
106	default:
107		fatalx("unexpected signal");
108	}
109}
110
111/* route decision engine */
112pid_t
113rde(struct ospfd_conf *xconf, int pipe_parent2rde[2], int pipe_ospfe2rde[2],
114    int pipe_parent2ospfe[2])
115{
116	struct event		 ev_sigint, ev_sigterm;
117	struct timeval		 now;
118	struct passwd		*pw;
119	struct redistribute	*r;
120	pid_t			 pid;
121
122	switch (pid = fork()) {
123	case -1:
124		fatal("cannot fork");
125		/* NOTREACHED */
126	case 0:
127		break;
128	default:
129		return (pid);
130	}
131
132	rdeconf = xconf;
133
134	if ((pw = getpwnam(OSPF6D_USER)) == NULL)
135		fatal("getpwnam");
136
137	if (chroot(pw->pw_dir) == -1)
138		fatal("chroot");
139	if (chdir("/") == -1)
140		fatal("chdir(\"/\")");
141
142	setproctitle("route decision engine");
143	ospfd_process = PROC_RDE_ENGINE;
144
145	if (setgroups(1, &pw->pw_gid) ||
146	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
147	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
148		fatal("can't drop privileges");
149
150	event_init();
151	rde_nbr_init(NBR_HASHSIZE);
152	lsa_init(&asext_tree);
153
154	/* setup signal handler */
155	signal_set(&ev_sigint, SIGINT, rde_sig_handler, NULL);
156	signal_set(&ev_sigterm, SIGTERM, rde_sig_handler, NULL);
157	signal_add(&ev_sigint, NULL);
158	signal_add(&ev_sigterm, NULL);
159	signal(SIGPIPE, SIG_IGN);
160	signal(SIGHUP, SIG_IGN);
161
162	/* setup pipes */
163	close(pipe_ospfe2rde[0]);
164	close(pipe_parent2rde[0]);
165	close(pipe_parent2ospfe[0]);
166	close(pipe_parent2ospfe[1]);
167
168	if ((iev_ospfe = malloc(sizeof(struct imsgev))) == NULL ||
169	    (iev_main = malloc(sizeof(struct imsgev))) == NULL)
170		fatal(NULL);
171	imsg_init(&iev_ospfe->ibuf, pipe_ospfe2rde[1]);
172	iev_ospfe->handler = rde_dispatch_imsg;
173	imsg_init(&iev_main->ibuf, pipe_parent2rde[1]);
174	iev_main->handler = rde_dispatch_parent;
175
176	/* setup event handler */
177	iev_ospfe->events = EV_READ;
178	event_set(&iev_ospfe->ev, iev_ospfe->ibuf.fd, iev_ospfe->events,
179	    iev_ospfe->handler, iev_ospfe);
180	event_add(&iev_ospfe->ev, NULL);
181
182	iev_main->events = EV_READ;
183	event_set(&iev_main->ev, iev_main->ibuf.fd, iev_main->events,
184	    iev_main->handler, iev_main);
185	event_add(&iev_main->ev, NULL);
186
187	evtimer_set(&rdeconf->ev, spf_timer, rdeconf);
188	cand_list_init();
189	rt_init();
190
191	while ((r = SIMPLEQ_FIRST(&rdeconf->redist_list)) != NULL) {
192		SIMPLEQ_REMOVE_HEAD(&rdeconf->redist_list, entry);
193		free(r);
194	}
195
196	gettimeofday(&now, NULL);
197	rdeconf->uptime = now.tv_sec;
198
199	event_dispatch();
200
201	rde_shutdown();
202	/* NOTREACHED */
203
204	return (0);
205}
206
207void
208rde_shutdown(void)
209{
210	struct area	*a;
211
212	stop_spf_timer(rdeconf);
213	cand_list_clr();
214	rt_clear();
215
216	while ((a = LIST_FIRST(&rdeconf->area_list)) != NULL) {
217		LIST_REMOVE(a, entry);
218		area_del(a);
219	}
220	rde_nbr_free();
221
222	msgbuf_clear(&iev_ospfe->ibuf.w);
223	free(iev_ospfe);
224	msgbuf_clear(&iev_main->ibuf.w);
225	free(iev_main);
226	free(rdeconf);
227
228	log_info("route decision engine exiting");
229	_exit(0);
230}
231
232int
233rde_imsg_compose_ospfe(int type, u_int32_t peerid, pid_t pid, void *data,
234    u_int16_t datalen)
235{
236	return (imsg_compose_event(iev_ospfe, type, peerid, pid, -1,
237	    data, datalen));
238}
239
240/* ARGSUSED */
241void
242rde_dispatch_imsg(int fd, short event, void *bula)
243{
244	struct imsgev		*iev = bula;
245	struct imsgbuf		*ibuf = &iev->ibuf;
246	struct imsg		 imsg;
247	struct in_addr		 aid;
248	struct ls_req_hdr	 req_hdr;
249	struct lsa_hdr		 lsa_hdr, *db_hdr;
250	struct rde_nbr		 rn, *nbr;
251	struct timespec		 tp;
252	struct lsa		*lsa;
253	struct area		*area;
254	struct vertex		*v;
255	struct iface		*iface, *ifp;
256	char			*buf;
257	ssize_t			 n;
258	time_t			 now;
259	int			 r, state, self, shut = 0, verbose;
260	u_int16_t		 l;
261
262	if (event & EV_READ) {
263		if ((n = imsg_read(ibuf)) == -1)
264			fatal("imsg_read error");
265		if (n == 0)	/* connection closed */
266			shut = 1;
267	}
268	if (event & EV_WRITE) {
269		if (msgbuf_write(&ibuf->w) == -1)
270			fatal("msgbuf_write");
271	}
272
273	clock_gettime(CLOCK_MONOTONIC, &tp);
274	now = tp.tv_sec;
275
276	for (;;) {
277		if ((n = imsg_get(ibuf, &imsg)) == -1)
278			fatal("rde_dispatch_imsg: imsg_read error");
279		if (n == 0)
280			break;
281
282		switch (imsg.hdr.type) {
283		case IMSG_NEIGHBOR_UP:
284			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(rn))
285				fatalx("invalid size of OE request");
286			memcpy(&rn, imsg.data, sizeof(rn));
287
288			if (rde_nbr_new(imsg.hdr.peerid, &rn) == NULL)
289				fatalx("rde_dispatch_imsg: "
290				    "neighbor already exists");
291			break;
292		case IMSG_NEIGHBOR_DOWN:
293			rde_nbr_del(rde_nbr_find(imsg.hdr.peerid));
294			break;
295		case IMSG_NEIGHBOR_CHANGE:
296			if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(state))
297				fatalx("invalid size of OE request");
298			memcpy(&state, imsg.data, sizeof(state));
299
300			nbr = rde_nbr_find(imsg.hdr.peerid);
301			if (nbr == NULL)
302				break;
303
304			if (state != nbr->state &&
305			    (nbr->state & NBR_STA_FULL ||
306			    state & NBR_STA_FULL)) {
307				nbr->state = state;
308				area_track(nbr->area, state);
309				orig_intra_area_prefix_lsas(nbr->area);
310			}
311
312			nbr->state = state;
313			if (nbr->state & NBR_STA_FULL)
314				rde_req_list_free(nbr);
315			break;
316		case IMSG_DB_SNAPSHOT:
317			nbr = rde_nbr_find(imsg.hdr.peerid);
318			if (nbr == NULL)
319				break;
320
321			lsa_snap(nbr, imsg.hdr.peerid);
322
323			imsg_compose_event(iev_ospfe, IMSG_DB_END, imsg.hdr.peerid,
324			    0, -1, NULL, 0);
325			break;
326		case IMSG_DD:
327			nbr = rde_nbr_find(imsg.hdr.peerid);
328			if (nbr == NULL)
329				break;
330
331			buf = imsg.data;
332			for (l = imsg.hdr.len - IMSG_HEADER_SIZE;
333			    l >= sizeof(lsa_hdr); l -= sizeof(lsa_hdr)) {
334				memcpy(&lsa_hdr, buf, sizeof(lsa_hdr));
335				buf += sizeof(lsa_hdr);
336
337				v = lsa_find(nbr->iface, lsa_hdr.type,
338				    lsa_hdr.ls_id, lsa_hdr.adv_rtr);
339				if (v == NULL)
340					db_hdr = NULL;
341				else
342					db_hdr = &v->lsa->hdr;
343
344				if (lsa_newer(&lsa_hdr, db_hdr) > 0) {
345					/*
346					 * only request LSAs that are
347					 * newer or missing
348					 */
349					rde_req_list_add(nbr, &lsa_hdr);
350					imsg_compose_event(iev_ospfe, IMSG_DD,
351					    imsg.hdr.peerid, 0, -1, &lsa_hdr,
352					    sizeof(lsa_hdr));
353				}
354			}
355			if (l != 0)
356				log_warnx("rde_dispatch_imsg: peerid %lu, "
357				    "trailing garbage in Database Description "
358				    "packet", imsg.hdr.peerid);
359
360			imsg_compose_event(iev_ospfe, IMSG_DD_END,
361			    imsg.hdr.peerid, 0, -1, NULL, 0);
362			break;
363		case IMSG_LS_REQ:
364			nbr = rde_nbr_find(imsg.hdr.peerid);
365			if (nbr == NULL)
366				break;
367
368			buf = imsg.data;
369			for (l = imsg.hdr.len - IMSG_HEADER_SIZE;
370			    l >= sizeof(req_hdr); l -= sizeof(req_hdr)) {
371				memcpy(&req_hdr, buf, sizeof(req_hdr));
372				buf += sizeof(req_hdr);
373
374				if ((v = lsa_find(nbr->iface,
375				    req_hdr.type, req_hdr.ls_id,
376				    req_hdr.adv_rtr)) == NULL) {
377					imsg_compose_event(iev_ospfe,
378					    IMSG_LS_BADREQ,
379					    imsg.hdr.peerid, 0, -1, NULL, 0);
380					continue;
381				}
382				imsg_compose_event(iev_ospfe, IMSG_LS_UPD,
383				    imsg.hdr.peerid, 0, -1, v->lsa,
384				    ntohs(v->lsa->hdr.len));
385			}
386			if (l != 0)
387				log_warnx("rde_dispatch_imsg: peerid %lu, "
388				    "trailing garbage in LS Request "
389				    "packet", imsg.hdr.peerid);
390			break;
391		case IMSG_LS_UPD:
392			nbr = rde_nbr_find(imsg.hdr.peerid);
393			if (nbr == NULL)
394				break;
395
396			lsa = malloc(imsg.hdr.len - IMSG_HEADER_SIZE);
397			if (lsa == NULL)
398				fatal(NULL);
399			memcpy(lsa, imsg.data, imsg.hdr.len - IMSG_HEADER_SIZE);
400
401			if (!lsa_check(nbr, lsa,
402			    imsg.hdr.len - IMSG_HEADER_SIZE)) {
403				free(lsa);
404				break;
405			}
406
407			v = lsa_find(nbr->iface, lsa->hdr.type, lsa->hdr.ls_id,
408				    lsa->hdr.adv_rtr);
409			if (v == NULL)
410				db_hdr = NULL;
411			else
412				db_hdr = &v->lsa->hdr;
413
414			if (nbr->self) {
415				lsa_merge(nbr, lsa, v);
416				/* lsa_merge frees the right lsa */
417				break;
418			}
419
420			r = lsa_newer(&lsa->hdr, db_hdr);
421			if (r > 0) {
422				/* new LSA newer than DB */
423				if (v && v->flooded &&
424				    v->changed + MIN_LS_ARRIVAL >= now) {
425					free(lsa);
426					break;
427				}
428
429				rde_req_list_del(nbr, &lsa->hdr);
430
431				self = lsa_self(lsa);
432				if (self) {
433					if (v == NULL)
434						/* LSA is no longer announced,
435						 * remove by premature aging. */
436						lsa_flush(nbr, lsa);
437					else
438						lsa_reflood(v, lsa);
439				} else if (lsa_add(nbr, lsa))
440					/* delayed lsa, don't flood yet */
441					break;
442
443				/* flood and perhaps ack LSA */
444				imsg_compose_event(iev_ospfe, IMSG_LS_FLOOD,
445				    imsg.hdr.peerid, 0, -1, lsa,
446				    ntohs(lsa->hdr.len));
447
448				/* reflood self originated LSA */
449				if (self && v)
450					imsg_compose_event(iev_ospfe,
451					    IMSG_LS_FLOOD,
452					    v->peerid, 0, -1, v->lsa,
453					    ntohs(v->lsa->hdr.len));
454				/* new LSA was not added so free it */
455				if (self)
456					free(lsa);
457			} else if (r < 0) {
458				/*
459				 * point 6 of "The Flooding Procedure"
460				 * We are violating the RFC here because
461				 * it does not make sense to reset a session
462				 * because an equal LSA is already in the table.
463				 * Only if the LSA sent is older than the one
464				 * in the table we should reset the session.
465				 */
466				if (rde_req_list_exists(nbr, &lsa->hdr)) {
467					imsg_compose_event(iev_ospfe,
468					    IMSG_LS_BADREQ,
469					    imsg.hdr.peerid, 0, -1, NULL, 0);
470					free(lsa);
471					break;
472				}
473
474				/* lsa no longer needed */
475				free(lsa);
476
477				/* new LSA older than DB */
478				if (ntohl(db_hdr->seq_num) == MAX_SEQ_NUM &&
479				    ntohs(db_hdr->age) == MAX_AGE)
480					/* seq-num wrap */
481					break;
482
483				if (v->changed + MIN_LS_ARRIVAL >= now)
484					break;
485
486				/* directly send current LSA, no ack */
487				imsg_compose_event(iev_ospfe, IMSG_LS_UPD,
488				    imsg.hdr.peerid, 0, -1, v->lsa,
489				    ntohs(v->lsa->hdr.len));
490			} else {
491				/* LSA equal send direct ack */
492				imsg_compose_event(iev_ospfe, IMSG_LS_ACK,
493				    imsg.hdr.peerid, 0, -1, &lsa->hdr,
494				    sizeof(lsa->hdr));
495				free(lsa);
496			}
497			break;
498		case IMSG_LS_MAXAGE:
499			nbr = rde_nbr_find(imsg.hdr.peerid);
500			if (nbr == NULL)
501				break;
502
503			if (imsg.hdr.len != IMSG_HEADER_SIZE +
504			    sizeof(struct lsa_hdr))
505				fatalx("invalid size of OE request");
506			memcpy(&lsa_hdr, imsg.data, sizeof(lsa_hdr));
507
508			if (rde_nbr_loading(nbr->area))
509				break;
510
511			v = lsa_find(nbr->iface, lsa_hdr.type, lsa_hdr.ls_id,
512				    lsa_hdr.adv_rtr);
513			if (v == NULL)
514				db_hdr = NULL;
515			else
516				db_hdr = &v->lsa->hdr;
517
518			/*
519			 * only delete LSA if the one in the db is not newer
520			 */
521			if (lsa_newer(db_hdr, &lsa_hdr) <= 0)
522				lsa_del(nbr, &lsa_hdr);
523			break;
524		case IMSG_CTL_SHOW_DATABASE:
525		case IMSG_CTL_SHOW_DB_EXT:
526		case IMSG_CTL_SHOW_DB_LINK:
527		case IMSG_CTL_SHOW_DB_NET:
528		case IMSG_CTL_SHOW_DB_RTR:
529		case IMSG_CTL_SHOW_DB_INTRA:
530		case IMSG_CTL_SHOW_DB_SELF:
531		case IMSG_CTL_SHOW_DB_SUM:
532		case IMSG_CTL_SHOW_DB_ASBR:
533			if (imsg.hdr.len != IMSG_HEADER_SIZE &&
534			    imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(aid)) {
535				log_warnx("rde_dispatch_imsg: wrong imsg len");
536				break;
537			}
538			if (imsg.hdr.len == IMSG_HEADER_SIZE) {
539				LIST_FOREACH(area, &rdeconf->area_list, entry) {
540					rde_dump_area(area, imsg.hdr.type,
541					    imsg.hdr.pid);
542				}
543				lsa_dump(&asext_tree, imsg.hdr.type,
544				    imsg.hdr.pid);
545			} else {
546				memcpy(&aid, imsg.data, sizeof(aid));
547				if ((area = area_find(rdeconf, aid)) != NULL) {
548					rde_dump_area(area, imsg.hdr.type,
549					    imsg.hdr.pid);
550					if (!area->stub)
551						lsa_dump(&asext_tree,
552						    imsg.hdr.type,
553						    imsg.hdr.pid);
554				}
555			}
556			imsg_compose_event(iev_ospfe, IMSG_CTL_END, 0,
557			    imsg.hdr.pid, -1, NULL, 0);
558			break;
559		case IMSG_CTL_SHOW_RIB:
560			LIST_FOREACH(area, &rdeconf->area_list, entry) {
561				imsg_compose_event(iev_ospfe, IMSG_CTL_AREA,
562				    0, imsg.hdr.pid, -1, area, sizeof(*area));
563
564				rt_dump(area->id, imsg.hdr.pid, RIB_RTR);
565				rt_dump(area->id, imsg.hdr.pid, RIB_NET);
566			}
567			aid.s_addr = 0;
568			rt_dump(aid, imsg.hdr.pid, RIB_EXT);
569
570			imsg_compose_event(iev_ospfe, IMSG_CTL_END, 0,
571			    imsg.hdr.pid, -1, NULL, 0);
572			break;
573		case IMSG_CTL_SHOW_SUM:
574			rde_send_summary(imsg.hdr.pid);
575			LIST_FOREACH(area, &rdeconf->area_list, entry)
576				rde_send_summary_area(area, imsg.hdr.pid);
577			imsg_compose_event(iev_ospfe, IMSG_CTL_END, 0,
578			    imsg.hdr.pid, -1, NULL, 0);
579			break;
580		case IMSG_IFINFO:
581			if (imsg.hdr.len != IMSG_HEADER_SIZE +
582			    sizeof(struct iface))
583				fatalx("IFINFO imsg with wrong len");
584
585			ifp = imsg.data;
586
587			iface = if_find(ifp->ifindex);
588			if (iface == NULL)
589				fatalx("interface lost in rde");
590			iface->flags = ifp->flags;
591			iface->linkstate = ifp->linkstate;
592			iface->nh_reachable = ifp->nh_reachable;
593			if (iface->state != ifp->state) {
594				iface->state = ifp->state;
595				area = area_find(rdeconf, iface->area_id);
596				if (!area)
597					fatalx("interface lost area");
598				orig_intra_area_prefix_lsas(area);
599			}
600			break;
601		case IMSG_CTL_LOG_VERBOSE:
602			/* already checked by ospfe */
603			memcpy(&verbose, imsg.data, sizeof(verbose));
604			log_verbose(verbose);
605			break;
606		default:
607			log_debug("rde_dispatch_imsg: unexpected imsg %d",
608			    imsg.hdr.type);
609			break;
610		}
611		imsg_free(&imsg);
612	}
613	if (!shut)
614		imsg_event_add(iev);
615	else {
616		/* this pipe is dead, so remove the event handler */
617		event_del(&iev->ev);
618		event_loopexit(NULL);
619	}
620}
621
622/* ARGSUSED */
623void
624rde_dispatch_parent(int fd, short event, void *bula)
625{
626	static struct area	*narea;
627	struct area		*area;
628	struct iface		*iface;
629	struct ifaddrchange	*ifc;
630	struct iface_addr	*ia, *nia;
631	struct imsg		 imsg;
632	struct kroute		 kr;
633	struct rroute		 rr;
634	struct imsgev		*iev = bula;
635	struct imsgbuf		*ibuf = &iev->ibuf;
636	struct lsa		*lsa;
637	struct vertex		*v;
638	struct rt_node		*rn;
639	ssize_t			 n;
640	int			 shut = 0;
641	unsigned int		 ifindex;
642
643	if (event & EV_READ) {
644		if ((n = imsg_read(ibuf)) == -1)
645			fatal("imsg_read error");
646		if (n == 0)	/* connection closed */
647			shut = 1;
648	}
649	if (event & EV_WRITE) {
650		if (msgbuf_write(&ibuf->w) == -1)
651			fatal("msgbuf_write");
652	}
653
654	for (;;) {
655		if ((n = imsg_get(ibuf, &imsg)) == -1)
656			fatal("rde_dispatch_parent: imsg_read error");
657		if (n == 0)
658			break;
659
660		switch (imsg.hdr.type) {
661		case IMSG_NETWORK_ADD:
662			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(rr)) {
663				log_warnx("rde_dispatch_parent: "
664				    "wrong imsg len");
665				break;
666			}
667			memcpy(&rr, imsg.data, sizeof(rr));
668
669			if ((lsa = rde_asext_get(&rr)) != NULL) {
670				v = lsa_find(NULL, lsa->hdr.type,
671				    lsa->hdr.ls_id, lsa->hdr.adv_rtr);
672
673				lsa_merge(nbrself, lsa, v);
674			}
675			break;
676		case IMSG_NETWORK_DEL:
677			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(rr)) {
678				log_warnx("rde_dispatch_parent: "
679				    "wrong imsg len");
680				break;
681			}
682			memcpy(&rr, imsg.data, sizeof(rr));
683
684			if ((lsa = rde_asext_put(&rr)) != NULL) {
685				v = lsa_find(NULL, lsa->hdr.type,
686				    lsa->hdr.ls_id, lsa->hdr.adv_rtr);
687
688				/*
689				 * if v == NULL no LSA is in the table and
690				 * nothing has to be done.
691				 */
692				if (v)
693					lsa_merge(nbrself, lsa, v);
694				else
695					free(lsa);
696			}
697			break;
698		case IMSG_KROUTE_GET:
699			if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(kr)) {
700				log_warnx("rde_dispatch_parent: "
701				    "wrong imsg len");
702				break;
703			}
704			memcpy(&kr, imsg.data, sizeof(kr));
705
706			if ((rn = rt_find(&kr.prefix, kr.prefixlen,
707			    DT_NET)) != NULL)
708				rde_send_change_kroute(rn);
709			else
710				/* should not happen */
711				imsg_compose_event(iev_main, IMSG_KROUTE_DELETE, 0,
712				    0, -1, &kr, sizeof(kr));
713			break;
714		case IMSG_IFADD:
715			if ((iface = malloc(sizeof(struct iface))) == NULL)
716				fatal(NULL);
717			memcpy(iface, imsg.data, sizeof(struct iface));
718
719			LIST_INIT(&iface->nbr_list);
720			TAILQ_INIT(&iface->ls_ack_list);
721			RB_INIT(&iface->lsa_tree);
722
723			area = area_find(rdeconf, iface->area_id);
724			LIST_INSERT_HEAD(&area->iface_list, iface, entry);
725			break;
726		case IMSG_IFDELETE:
727			if (imsg.hdr.len != IMSG_HEADER_SIZE +
728			    sizeof(ifindex))
729				fatalx("IFDELETE imsg with wrong len");
730
731			memcpy(&ifindex, imsg.data, sizeof(ifindex));
732			iface = if_find(ifindex);
733			if (iface == NULL)
734				fatalx("interface lost in ospfe");
735
736			LIST_REMOVE(iface, entry);
737			if_del(iface);
738			break;
739		case IMSG_IFADDRNEW:
740			if (imsg.hdr.len != IMSG_HEADER_SIZE +
741			    sizeof(struct ifaddrchange))
742				fatalx("IFADDRNEW imsg with wrong len");
743			ifc = imsg.data;
744
745			iface = if_find(ifc->ifindex);
746			if (iface == NULL)
747				fatalx("IFADDRNEW interface lost in rde");
748
749			if ((ia = calloc(1, sizeof(struct iface_addr))) ==
750			    NULL)
751				fatal("rde_dispatch_parent IFADDRNEW");
752			ia->addr = ifc->addr;
753			ia->dstbrd = ifc->dstbrd;
754			ia->prefixlen = ifc->prefixlen;
755
756			TAILQ_INSERT_TAIL(&iface->ifa_list, ia, entry);
757			area = area_find(rdeconf, iface->area_id);
758			if (area)
759				orig_intra_area_prefix_lsas(area);
760			break;
761		case IMSG_IFADDRDEL:
762			if (imsg.hdr.len != IMSG_HEADER_SIZE +
763			    sizeof(struct ifaddrchange))
764				fatalx("IFADDRDEL imsg with wrong len");
765			ifc = imsg.data;
766
767			iface = if_find(ifc->ifindex);
768			if (iface == NULL)
769				fatalx("IFADDRDEL interface lost in rde");
770
771			for (ia = TAILQ_FIRST(&iface->ifa_list); ia != NULL;
772			    ia = nia) {
773				nia = TAILQ_NEXT(ia, entry);
774
775				if (IN6_ARE_ADDR_EQUAL(&ia->addr,
776				    &ifc->addr)) {
777					TAILQ_REMOVE(&iface->ifa_list, ia,
778					    entry);
779					free(ia);
780					break;
781				}
782			}
783			area = area_find(rdeconf, iface->area_id);
784			if (area)
785				orig_intra_area_prefix_lsas(area);
786			break;
787		case IMSG_RECONF_CONF:
788			if ((nconf = malloc(sizeof(struct ospfd_conf))) ==
789			    NULL)
790				fatal(NULL);
791			memcpy(nconf, imsg.data, sizeof(struct ospfd_conf));
792
793			LIST_INIT(&nconf->area_list);
794			LIST_INIT(&nconf->cand_list);
795			break;
796		case IMSG_RECONF_AREA:
797			if ((narea = area_new()) == NULL)
798				fatal(NULL);
799			memcpy(narea, imsg.data, sizeof(struct area));
800
801			LIST_INIT(&narea->iface_list);
802			LIST_INIT(&narea->nbr_list);
803			RB_INIT(&narea->lsa_tree);
804
805			LIST_INSERT_HEAD(&nconf->area_list, narea, entry);
806			break;
807		case IMSG_RECONF_END:
808			merge_config(rdeconf, nconf);
809			nconf = NULL;
810			break;
811		default:
812			log_debug("rde_dispatch_parent: unexpected imsg %d",
813			    imsg.hdr.type);
814			break;
815		}
816		imsg_free(&imsg);
817	}
818	if (!shut)
819		imsg_event_add(iev);
820	else {
821		/* this pipe is dead, so remove the event handler */
822		event_del(&iev->ev);
823		event_loopexit(NULL);
824	}
825}
826
827void
828rde_dump_area(struct area *area, int imsg_type, pid_t pid)
829{
830	struct iface	*iface;
831
832	/* dump header */
833	imsg_compose_event(iev_ospfe, IMSG_CTL_AREA, 0, pid, -1,
834	    area, sizeof(*area));
835
836	/* dump link local lsa */
837	LIST_FOREACH(iface, &area->iface_list, entry) {
838		imsg_compose_event(iev_ospfe, IMSG_CTL_IFACE,
839		    0, pid, -1, iface, sizeof(*iface));
840		lsa_dump(&iface->lsa_tree, imsg_type, pid);
841	}
842
843	/* dump area lsa */
844	lsa_dump(&area->lsa_tree, imsg_type, pid);
845}
846
847u_int32_t
848rde_router_id(void)
849{
850	return (rdeconf->rtr_id.s_addr);
851}
852
853void
854rde_send_change_kroute(struct rt_node *r)
855{
856	struct kroute		 kr;
857	struct rt_nexthop	*rn;
858
859	TAILQ_FOREACH(rn, &r->nexthop, entry) {
860		if (!rn->invalid)
861			break;
862	}
863	if (!rn)
864		fatalx("rde_send_change_kroute: no valid nexthop found");
865
866	bzero(&kr, sizeof(kr));
867	kr.prefix = r->prefix;
868	kr.nexthop = rn->nexthop;
869	if (IN6_IS_ADDR_LINKLOCAL(&rn->nexthop) ||
870	    IN6_IS_ADDR_MC_LINKLOCAL(&rn->nexthop))
871		kr.scope = rn->ifindex;
872	kr.ifindex = rn->ifindex;
873	kr.prefixlen = r->prefixlen;
874	kr.ext_tag = r->ext_tag;
875
876	imsg_compose_event(iev_main, IMSG_KROUTE_CHANGE, 0, 0, -1,
877	    &kr, sizeof(kr));
878}
879
880void
881rde_send_delete_kroute(struct rt_node *r)
882{
883	struct kroute	 kr;
884
885	bzero(&kr, sizeof(kr));
886	kr.prefix = r->prefix;
887	kr.prefixlen = r->prefixlen;
888
889	imsg_compose_event(iev_main, IMSG_KROUTE_DELETE, 0, 0, -1,
890	    &kr, sizeof(kr));
891}
892
893void
894rde_send_summary(pid_t pid)
895{
896	static struct ctl_sum	 sumctl;
897	struct timeval		 now;
898	struct area		*area;
899	struct vertex		*v;
900
901	bzero(&sumctl, sizeof(struct ctl_sum));
902
903	sumctl.rtr_id.s_addr = rde_router_id();
904	sumctl.spf_delay = rdeconf->spf_delay;
905	sumctl.spf_hold_time = rdeconf->spf_hold_time;
906
907	LIST_FOREACH(area, &rdeconf->area_list, entry)
908		sumctl.num_area++;
909
910	RB_FOREACH(v, lsa_tree, &asext_tree)
911		sumctl.num_ext_lsa++;
912
913	gettimeofday(&now, NULL);
914	if (rdeconf->uptime < now.tv_sec)
915		sumctl.uptime = now.tv_sec - rdeconf->uptime;
916	else
917		sumctl.uptime = 0;
918
919	rde_imsg_compose_ospfe(IMSG_CTL_SHOW_SUM, 0, pid, &sumctl,
920	    sizeof(sumctl));
921}
922
923void
924rde_send_summary_area(struct area *area, pid_t pid)
925{
926	static struct ctl_sum_area	 sumareactl;
927	struct iface			*iface;
928	struct rde_nbr			*nbr;
929	struct lsa_tree			*tree = &area->lsa_tree;
930	struct vertex			*v;
931
932	bzero(&sumareactl, sizeof(struct ctl_sum_area));
933
934	sumareactl.area.s_addr = area->id.s_addr;
935	sumareactl.num_spf_calc = area->num_spf_calc;
936
937	LIST_FOREACH(iface, &area->iface_list, entry)
938		sumareactl.num_iface++;
939
940	LIST_FOREACH(nbr, &area->nbr_list, entry)
941		if (nbr->state == NBR_STA_FULL && !nbr->self)
942			sumareactl.num_adj_nbr++;
943
944	RB_FOREACH(v, lsa_tree, tree)
945		sumareactl.num_lsa++;
946
947	rde_imsg_compose_ospfe(IMSG_CTL_SHOW_SUM_AREA, 0, pid, &sumareactl,
948	    sizeof(sumareactl));
949}
950
951LIST_HEAD(rde_nbr_head, rde_nbr);
952
953struct nbr_table {
954	struct rde_nbr_head	*hashtbl;
955	u_int32_t		 hashmask;
956} rdenbrtable;
957
958#define RDE_NBR_HASH(x)		\
959	&rdenbrtable.hashtbl[(x) & rdenbrtable.hashmask]
960
961void
962rde_nbr_init(u_int32_t hashsize)
963{
964	struct rde_nbr_head	*head;
965	u_int32_t		 hs, i;
966
967	for (hs = 1; hs < hashsize; hs <<= 1)
968		;
969	rdenbrtable.hashtbl = calloc(hs, sizeof(struct rde_nbr_head));
970	if (rdenbrtable.hashtbl == NULL)
971		fatal("rde_nbr_init");
972
973	for (i = 0; i < hs; i++)
974		LIST_INIT(&rdenbrtable.hashtbl[i]);
975
976	rdenbrtable.hashmask = hs - 1;
977
978	if ((nbrself = calloc(1, sizeof(*nbrself))) == NULL)
979		fatal("rde_nbr_init");
980
981	nbrself->id.s_addr = rde_router_id();
982	nbrself->peerid = NBR_IDSELF;
983	nbrself->state = NBR_STA_DOWN;
984	nbrself->self = 1;
985	head = RDE_NBR_HASH(NBR_IDSELF);
986	LIST_INSERT_HEAD(head, nbrself, hash);
987}
988
989void
990rde_nbr_free(void)
991{
992	free(nbrself);
993	free(rdenbrtable.hashtbl);
994}
995
996struct rde_nbr *
997rde_nbr_find(u_int32_t peerid)
998{
999	struct rde_nbr_head	*head;
1000	struct rde_nbr		*nbr;
1001
1002	head = RDE_NBR_HASH(peerid);
1003
1004	LIST_FOREACH(nbr, head, hash) {
1005		if (nbr->peerid == peerid)
1006			return (nbr);
1007	}
1008
1009	return (NULL);
1010}
1011
1012struct rde_nbr *
1013rde_nbr_new(u_int32_t peerid, struct rde_nbr *new)
1014{
1015	struct rde_nbr_head	*head;
1016	struct rde_nbr		*nbr;
1017	struct area		*area;
1018	struct iface		*iface;
1019
1020	if (rde_nbr_find(peerid))
1021		return (NULL);
1022	if ((area = area_find(rdeconf, new->area_id)) == NULL)
1023		fatalx("rde_nbr_new: unknown area");
1024
1025	LIST_FOREACH(iface, &area->iface_list, entry) {
1026		if (iface->ifindex == new->ifindex)
1027			break;
1028	}
1029	if (iface == NULL)
1030		fatalx("rde_nbr_new: unknown interface");
1031
1032	if ((nbr = calloc(1, sizeof(*nbr))) == NULL)
1033		fatal("rde_nbr_new");
1034
1035	memcpy(nbr, new, sizeof(*nbr));
1036	nbr->peerid = peerid;
1037	nbr->area = area;
1038	nbr->iface = iface;
1039
1040	TAILQ_INIT(&nbr->req_list);
1041
1042	head = RDE_NBR_HASH(peerid);
1043	LIST_INSERT_HEAD(head, nbr, hash);
1044	LIST_INSERT_HEAD(&area->nbr_list, nbr, entry);
1045
1046	return (nbr);
1047}
1048
1049void
1050rde_nbr_del(struct rde_nbr *nbr)
1051{
1052	if (nbr == NULL)
1053		return;
1054
1055	rde_req_list_free(nbr);
1056
1057	LIST_REMOVE(nbr, entry);
1058	LIST_REMOVE(nbr, hash);
1059
1060	free(nbr);
1061}
1062
1063int
1064rde_nbr_loading(struct area *area)
1065{
1066	struct rde_nbr		*nbr;
1067	int			 checkall = 0;
1068
1069	if (area == NULL) {
1070		area = LIST_FIRST(&rdeconf->area_list);
1071		checkall = 1;
1072	}
1073
1074	while (area != NULL) {
1075		LIST_FOREACH(nbr, &area->nbr_list, entry) {
1076			if (nbr->self)
1077				continue;
1078			if (nbr->state & NBR_STA_XCHNG ||
1079			    nbr->state & NBR_STA_LOAD)
1080				return (1);
1081		}
1082		if (!checkall)
1083			break;
1084		area = LIST_NEXT(area, entry);
1085	}
1086
1087	return (0);
1088}
1089
1090struct rde_nbr *
1091rde_nbr_self(struct area *area)
1092{
1093	struct rde_nbr		*nbr;
1094
1095	LIST_FOREACH(nbr, &area->nbr_list, entry)
1096		if (nbr->self)
1097			return (nbr);
1098
1099	/* this may not happen */
1100	fatalx("rde_nbr_self: area without self");
1101	return (NULL);
1102}
1103
1104/*
1105 * LSA req list
1106 */
1107void
1108rde_req_list_add(struct rde_nbr *nbr, struct lsa_hdr *lsa)
1109{
1110	struct rde_req_entry	*le;
1111
1112	if ((le = calloc(1, sizeof(*le))) == NULL)
1113		fatal("rde_req_list_add");
1114
1115	TAILQ_INSERT_TAIL(&nbr->req_list, le, entry);
1116	le->type = lsa->type;
1117	le->ls_id = lsa->ls_id;
1118	le->adv_rtr = lsa->adv_rtr;
1119}
1120
1121int
1122rde_req_list_exists(struct rde_nbr *nbr, struct lsa_hdr *lsa_hdr)
1123{
1124	struct rde_req_entry	*le;
1125
1126	TAILQ_FOREACH(le, &nbr->req_list, entry) {
1127		if ((lsa_hdr->type == le->type) &&
1128		    (lsa_hdr->ls_id == le->ls_id) &&
1129		    (lsa_hdr->adv_rtr == le->adv_rtr))
1130			return (1);
1131	}
1132	return (0);
1133}
1134
1135void
1136rde_req_list_del(struct rde_nbr *nbr, struct lsa_hdr *lsa_hdr)
1137{
1138	struct rde_req_entry	*le;
1139
1140	TAILQ_FOREACH(le, &nbr->req_list, entry) {
1141		if ((lsa_hdr->type == le->type) &&
1142		    (lsa_hdr->ls_id == le->ls_id) &&
1143		    (lsa_hdr->adv_rtr == le->adv_rtr)) {
1144			TAILQ_REMOVE(&nbr->req_list, le, entry);
1145			free(le);
1146			return;
1147		}
1148	}
1149}
1150
1151void
1152rde_req_list_free(struct rde_nbr *nbr)
1153{
1154	struct rde_req_entry	*le;
1155
1156	while ((le = TAILQ_FIRST(&nbr->req_list)) != NULL) {
1157		TAILQ_REMOVE(&nbr->req_list, le, entry);
1158		free(le);
1159	}
1160}
1161
1162/*
1163 * as-external LSA handling
1164 */
1165struct lsa *
1166rde_asext_get(struct rroute *rr)
1167{
1168	struct area		*area;
1169	struct iface		*iface;
1170	struct iface_addr	*ia;
1171	struct in6_addr		 addr;
1172
1173	LIST_FOREACH(area, &rdeconf->area_list, entry)
1174		LIST_FOREACH(iface, &area->iface_list, entry)
1175			TAILQ_FOREACH(ia, &iface->ifa_list, entry) {
1176				if (IN6_IS_ADDR_LINKLOCAL(&ia->addr))
1177					continue;
1178
1179				inet6applymask(&addr, &ia->addr,
1180				    rr->kr.prefixlen);
1181				if (!memcmp(&addr, &rr->kr.prefix,
1182				    sizeof(addr)) && rr->kr.prefixlen ==
1183				    ia->prefixlen) {
1184					/* already announced as Prefix LSA */
1185					log_debug("rde_asext_get: %s/%d is "
1186					    "part of prefix LSA",
1187					    log_in6addr(&rr->kr.prefix),
1188					    rr->kr.prefixlen);
1189					return (NULL);
1190				}
1191			}
1192
1193	/* update of seqnum is done by lsa_merge */
1194	return (orig_asext_lsa(rr, DEFAULT_AGE));
1195}
1196
1197struct lsa *
1198rde_asext_put(struct rroute *rr)
1199{
1200	/*
1201	 * just try to remove the LSA. If the prefix is announced as
1202	 * stub net LSA lsa_find() will fail later and nothing will happen.
1203	 */
1204
1205	/* remove by reflooding with MAX_AGE */
1206	return (orig_asext_lsa(rr, MAX_AGE));
1207}
1208
1209/*
1210 * summary LSA stuff
1211 */
1212void
1213rde_summary_update(struct rt_node *rte, struct area *area)
1214{
1215	struct vertex		*v = NULL;
1216//XXX	struct lsa		*lsa;
1217	u_int16_t		 type = 0;
1218
1219	/* first check if we actually need to announce this route */
1220	if (!(rte->d_type == DT_NET || rte->flags & OSPF_RTR_E))
1221		return;
1222	/* never create summaries for as-ext LSA */
1223	if (rte->p_type == PT_TYPE1_EXT || rte->p_type == PT_TYPE2_EXT)
1224		return;
1225	/* no need for summary LSA in the originating area */
1226	if (rte->area.s_addr == area->id.s_addr)
1227		return;
1228	/* no need to originate inter-area routes to the backbone */
1229	if (rte->p_type == PT_INTER_AREA && area->id.s_addr == INADDR_ANY)
1230		return;
1231	/* TODO nexthop check, nexthop part of area -> no summary */
1232	if (rte->cost >= LS_INFINITY)
1233		return;
1234	/* TODO AS border router specific checks */
1235	/* TODO inter-area network route stuff */
1236	/* TODO intra-area stuff -- condense LSA ??? */
1237
1238	if (rte->d_type == DT_NET) {
1239		type = LSA_TYPE_INTER_A_PREFIX;
1240	} else if (rte->d_type == DT_RTR) {
1241		type = LSA_TYPE_INTER_A_ROUTER;
1242	} else
1243
1244#if 0 /* XXX a lot todo */
1245	/* update lsa but only if it was changed */
1246	v = lsa_find(area, type, rte->prefix.s_addr, rde_router_id());
1247	lsa = orig_sum_lsa(rte, area, type, rte->invalid);
1248	lsa_merge(rde_nbr_self(area), lsa, v);
1249
1250	if (v == NULL)
1251		v = lsa_find(area, type, rte->prefix.s_addr, rde_router_id());
1252#endif
1253
1254	/* suppressed/deleted routes are not found in the second lsa_find */
1255	if (v)
1256		v->cost = rte->cost;
1257}
1258
1259/*
1260 * Functions for self-originated LSAs
1261 */
1262
1263/* Prefix LSAs have variable size. We have to be careful to copy the right
1264 * amount of bytes, and to realloc() the right amount of memory. */
1265void
1266append_prefix_lsa(struct lsa **lsa, u_int16_t *len, struct lsa_prefix *prefix)
1267{
1268	struct lsa_prefix	*copy;
1269	unsigned int		 lsa_prefix_len;
1270	unsigned int		 new_len;
1271	char			*new_lsa;
1272
1273	lsa_prefix_len = sizeof(struct lsa_prefix)
1274	    + LSA_PREFIXSIZE(prefix->prefixlen);
1275
1276	new_len = *len + lsa_prefix_len;
1277
1278	/* Make sure we have enough space for this prefix. */
1279	if ((new_lsa = realloc(*lsa, new_len)) == NULL)
1280		fatalx("append_prefix_lsa");
1281
1282	/* Append prefix to LSA. */
1283	copy = (struct lsa_prefix *)(new_lsa + *len);
1284	memcpy(copy, prefix, lsa_prefix_len);
1285	copy->metric = 0;
1286
1287	*lsa = (struct lsa *)new_lsa;
1288	*len = new_len;
1289}
1290
1291int
1292prefix_compare(struct prefix_node *a, struct prefix_node *b)
1293{
1294	struct lsa_prefix	*p;
1295	struct lsa_prefix	*q;
1296	int			 i;
1297	int			 len;
1298
1299	p = a->prefix;
1300	q = b->prefix;
1301
1302	len = MIN(LSA_PREFIXSIZE(p->prefixlen), LSA_PREFIXSIZE(q->prefixlen));
1303
1304	i = memcmp(p + 1, q + 1, len);
1305	if (i)
1306		return (i);
1307	if (p->prefixlen < q->prefixlen)
1308		return (-1);
1309	if (p->prefixlen > q->prefixlen)
1310		return (1);
1311	return (0);
1312}
1313
1314void
1315prefix_tree_add(struct prefix_tree *tree, struct lsa_link *lsa)
1316{
1317	struct prefix_node	*old;
1318	struct prefix_node	*new;
1319	struct in6_addr		 addr;
1320	unsigned int		 len;
1321	unsigned int		 i;
1322	char			*cur_prefix;
1323
1324	cur_prefix = (char *)(lsa + 1);
1325
1326	for (i = 0; i < ntohl(lsa->numprefix); i++) {
1327		if ((new = calloc(1, sizeof(*new))) == NULL)
1328			fatal("prefix_tree_add");
1329		new->prefix = (struct lsa_prefix *)cur_prefix;
1330
1331		len = sizeof(*new->prefix)
1332		    + LSA_PREFIXSIZE(new->prefix->prefixlen);
1333
1334		bzero(&addr, sizeof(addr));
1335		memcpy(&addr, new->prefix + 1,
1336		    LSA_PREFIXSIZE(new->prefix->prefixlen));
1337
1338		if (!(IN6_IS_ADDR_LINKLOCAL(&addr)) &&
1339		    (new->prefix->options & OSPF_PREFIX_NU) == 0 &&
1340		    (new->prefix->options & OSPF_PREFIX_LA) == 0) {
1341			old = RB_INSERT(prefix_tree, tree, new);
1342			if (old != NULL) {
1343				old->prefix->options |= new->prefix->options;
1344				free(new);
1345			}
1346		}
1347
1348		cur_prefix = cur_prefix + len;
1349	}
1350}
1351
1352RB_GENERATE(prefix_tree, prefix_node, entry, prefix_compare)
1353
1354struct lsa *
1355orig_intra_lsa_net(struct area *area, struct iface *iface, struct vertex *old)
1356{
1357	struct lsa		*lsa;
1358	struct vertex		*v;
1359	struct rde_nbr		*nbr;
1360	struct prefix_node	*node;
1361	struct prefix_tree	 tree;
1362	int			 num_full_nbr;
1363	u_int16_t		 len;
1364	u_int16_t		 numprefix;
1365
1366	log_debug("orig_intra_lsa_net: area %s, interface %s",
1367	    inet_ntoa(area->id), iface->name);
1368
1369	RB_INIT(&tree);
1370
1371	if (iface->state & IF_STA_DR) {
1372		num_full_nbr = 0;
1373		LIST_FOREACH(nbr, &area->nbr_list, entry) {
1374			if (nbr->self ||
1375			    nbr->iface->ifindex != iface->ifindex ||
1376			    (nbr->state & NBR_STA_FULL) == 0)
1377				continue;
1378			num_full_nbr++;
1379			v = lsa_find(iface, htons(LSA_TYPE_LINK),
1380			    htonl(nbr->iface_id), nbr->id.s_addr);
1381			if (v)
1382				prefix_tree_add(&tree, &v->lsa->data.link);
1383		}
1384		if (num_full_nbr == 0) {
1385			/* There are no adjacent neighbors on link.
1386			 * If a copy of this LSA already exists in DB,
1387			 * it needs to be flushed. orig_intra_lsa_rtr()
1388			 * will take care of prefixes configured on
1389			 * this interface. */
1390			if (!old)
1391				return NULL;
1392		} else {
1393			/* Add our own prefixes configured for this link. */
1394			v = lsa_find(iface, htons(LSA_TYPE_LINK),
1395			    htonl(iface->ifindex), rde_router_id());
1396			if (v)
1397				prefix_tree_add(&tree, &v->lsa->data.link);
1398		}
1399	/* Continue only if a copy of this LSA already exists in DB.
1400	 * It needs to be flushed. */
1401	} else if (!old)
1402		return NULL;
1403
1404	len = sizeof(struct lsa_hdr) + sizeof(struct lsa_intra_prefix);
1405	if ((lsa = calloc(1, len)) == NULL)
1406		fatal("orig_intra_lsa_net");
1407
1408	lsa->data.pref_intra.ref_type = htons(LSA_TYPE_NETWORK);
1409	lsa->data.pref_intra.ref_ls_id = htonl(iface->ifindex);
1410	lsa->data.pref_intra.ref_adv_rtr = rde_router_id();
1411
1412	numprefix = 0;
1413	RB_FOREACH(node, prefix_tree, &tree) {
1414		append_prefix_lsa(&lsa, &len, node->prefix);
1415		numprefix++;
1416	}
1417
1418	lsa->data.pref_intra.numprefix = htons(numprefix);
1419
1420	while (!RB_EMPTY(&tree))
1421		free(RB_REMOVE(prefix_tree, &tree, RB_ROOT(&tree)));
1422
1423	/* LSA header */
1424	/* If numprefix is zero, originate with MAX_AGE to flush LSA. */
1425	lsa->hdr.age = numprefix == 0 ? htons(MAX_AGE) : htons(DEFAULT_AGE);
1426	lsa->hdr.type = htons(LSA_TYPE_INTRA_A_PREFIX);
1427	lsa->hdr.ls_id = htonl(iface->ifindex);
1428	lsa->hdr.adv_rtr = rde_router_id();
1429	lsa->hdr.seq_num = htonl(INIT_SEQ_NUM);
1430	lsa->hdr.len = htons(len);
1431	lsa->hdr.ls_chksum = htons(iso_cksum(lsa, len, LS_CKSUM_OFFSET));
1432
1433	return lsa;
1434}
1435
1436struct lsa *
1437orig_intra_lsa_rtr(struct area *area, struct vertex *old)
1438{
1439	char			lsa_prefix_buf[sizeof(struct lsa_prefix)
1440				    + sizeof(struct in6_addr)];
1441	struct lsa		*lsa;
1442	struct lsa_prefix	*lsa_prefix;
1443	struct in6_addr		*prefix;
1444	struct iface		*iface;
1445	struct iface_addr	*ia;
1446	struct rde_nbr		*nbr;
1447	u_int16_t		 len;
1448	u_int16_t		 numprefix;
1449
1450	len = sizeof(struct lsa_hdr) + sizeof(struct lsa_intra_prefix);
1451	if ((lsa = calloc(1, len)) == NULL)
1452		fatal("orig_intra_lsa_rtr");
1453
1454	lsa->data.pref_intra.ref_type = htons(LSA_TYPE_ROUTER);
1455	lsa->data.pref_intra.ref_ls_id = 0;
1456	lsa->data.pref_intra.ref_adv_rtr = rde_router_id();
1457
1458	log_debug("orig_intra_lsa_rtr: area %s", inet_ntoa(area->id));
1459
1460	numprefix = 0;
1461	LIST_FOREACH(iface, &area->iface_list, entry) {
1462		if (iface->state & IF_STA_DOWN)
1463			continue;
1464
1465		/* Broadcast links with adjacencies are handled
1466		 * by orig_intra_lsa_net(), ignore. */
1467		if (iface->type == IF_TYPE_BROADCAST ||
1468		    iface->type == IF_TYPE_NBMA) {
1469			if (iface->state & IF_STA_WAITING)
1470				/* Skip, we're still waiting for
1471				 * adjacencies to form. */
1472				continue;
1473
1474			LIST_FOREACH(nbr, &area->nbr_list, entry)
1475				if (!nbr->self &&
1476				    nbr->iface->ifindex == iface->ifindex &&
1477				    nbr->state & NBR_STA_FULL)
1478					break;
1479			if (nbr)
1480				continue;
1481		}
1482
1483		lsa_prefix = (struct lsa_prefix *)lsa_prefix_buf;
1484
1485		TAILQ_FOREACH(ia, &iface->ifa_list, entry) {
1486			if (IN6_IS_ADDR_LINKLOCAL(&ia->addr))
1487				continue;
1488
1489			bzero(lsa_prefix_buf, sizeof(lsa_prefix_buf));
1490
1491			if (iface->type == IF_TYPE_POINTOMULTIPOINT ||
1492			    iface->state & IF_STA_LOOPBACK) {
1493				lsa_prefix->prefixlen = 128;
1494			} else {
1495				lsa_prefix->prefixlen = ia->prefixlen;
1496				lsa_prefix->metric = htons(iface->metric);
1497			}
1498
1499			if (lsa_prefix->prefixlen == 128)
1500				lsa_prefix->options |= OSPF_PREFIX_LA;
1501
1502			prefix = (struct in6_addr *)(lsa_prefix + 1);
1503			inet6applymask(prefix, &ia->addr,
1504			    lsa_prefix->prefixlen);
1505			append_prefix_lsa(&lsa, &len, lsa_prefix);
1506			numprefix++;
1507		}
1508
1509		/* TOD: Add prefixes of directly attached hosts, too */
1510		/* TOD: Add prefixes for virtual links */
1511	}
1512
1513	/* If no prefixes were included, continue only if a copy of this
1514	 * LSA already exists in DB. It needs to be flushed. */
1515	if (numprefix == 0 && !old) {
1516		free(lsa);
1517		return NULL;
1518	}
1519
1520	lsa->data.pref_intra.numprefix = htons(numprefix);
1521
1522	/* LSA header */
1523	/* If numprefix is zero, originate with MAX_AGE to flush LSA. */
1524	lsa->hdr.age = numprefix == 0 ? htons(MAX_AGE) : htons(DEFAULT_AGE);
1525	lsa->hdr.type = htons(LSA_TYPE_INTRA_A_PREFIX);
1526	lsa->hdr.ls_id = htonl(LS_ID_INTRA_RTR);
1527	lsa->hdr.adv_rtr = rde_router_id();
1528	lsa->hdr.seq_num = htonl(INIT_SEQ_NUM);
1529	lsa->hdr.len = htons(len);
1530	lsa->hdr.ls_chksum = htons(iso_cksum(lsa, len, LS_CKSUM_OFFSET));
1531
1532	return lsa;
1533}
1534
1535void
1536orig_intra_area_prefix_lsas(struct area *area)
1537{
1538	struct lsa	*lsa;
1539	struct vertex	*old;
1540	struct iface	*iface;
1541
1542	LIST_FOREACH(iface, &area->iface_list, entry) {
1543		if (iface->type == IF_TYPE_BROADCAST ||
1544		    iface->type == IF_TYPE_NBMA) {
1545			old = lsa_find(iface, htons(LSA_TYPE_INTRA_A_PREFIX),
1546			    htonl(iface->ifindex), rde_router_id());
1547			lsa = orig_intra_lsa_net(area, iface, old);
1548			if (lsa)
1549				lsa_merge(rde_nbr_self(area), lsa, old);
1550		}
1551	}
1552
1553	old = lsa_find_tree(&area->lsa_tree, htons(LSA_TYPE_INTRA_A_PREFIX),
1554		htonl(LS_ID_INTRA_RTR), rde_router_id());
1555	lsa = orig_intra_lsa_rtr(area, old);
1556	if (lsa)
1557		lsa_merge(rde_nbr_self(area), lsa, old);
1558}
1559
1560int
1561comp_asext(struct lsa *a, struct lsa *b)
1562{
1563	/* compare prefixes, if they are equal or not */
1564	if (a->data.asext.prefix.prefixlen != b->data.asext.prefix.prefixlen)
1565		return (-1);
1566	return (memcmp(
1567	    (char *)a + sizeof(struct lsa_hdr) + sizeof(struct lsa_asext),
1568	    (char *)b + sizeof(struct lsa_hdr) + sizeof(struct lsa_asext),
1569	    LSA_PREFIXSIZE(a->data.asext.prefix.prefixlen)));
1570}
1571
1572struct lsa *
1573orig_asext_lsa(struct rroute *rr, u_int16_t age)
1574{
1575	struct lsa	*lsa;
1576	u_int32_t	 ext_tag;
1577	u_int16_t	 len, ext_off = 0;
1578
1579	len = sizeof(struct lsa_hdr) + sizeof(struct lsa_asext) +
1580	    LSA_PREFIXSIZE(rr->kr.prefixlen);
1581
1582	/*
1583	 * nexthop -- on connected routes we are the nexthop,
1584	 * on all other cases we should announce the true nexthop
1585	 * unless that nexthop is outside of the ospf cloud.
1586	 * XXX for now we don't do this.
1587	 */
1588
1589	if (rr->kr.ext_tag) {
1590		ext_off = len;
1591		len += sizeof(ext_tag);
1592	}
1593	if ((lsa = calloc(1, len)) == NULL)
1594		fatal("orig_asext_lsa");
1595
1596	log_debug("orig_asext_lsa: %s/%d age %d",
1597	    log_in6addr(&rr->kr.prefix), rr->kr.prefixlen, age);
1598
1599	/* LSA header */
1600	lsa->hdr.age = htons(age);
1601	lsa->hdr.type = htons(LSA_TYPE_EXTERNAL);
1602	lsa->hdr.adv_rtr = rdeconf->rtr_id.s_addr;
1603	lsa->hdr.seq_num = htonl(INIT_SEQ_NUM);
1604	lsa->hdr.len = htons(len);
1605
1606	lsa->data.asext.metric = htonl(rr->metric);
1607	lsa->data.asext.prefix.prefixlen = rr->kr.prefixlen;
1608	memcpy((char *)lsa + sizeof(struct lsa_hdr) + sizeof(struct lsa_asext),
1609	    &rr->kr.prefix, LSA_PREFIXSIZE(rr->kr.prefixlen));
1610
1611	if (rr->kr.ext_tag) {
1612		lsa->data.asext.prefix.options |= LSA_ASEXT_T_FLAG;
1613		ext_tag = htonl(rr->kr.ext_tag);
1614		memcpy((char *)lsa + ext_off, &ext_tag, sizeof(ext_tag));
1615	}
1616
1617	lsa->hdr.ls_id = lsa_find_lsid(&asext_tree, lsa->hdr.type,
1618	    lsa->hdr.adv_rtr, comp_asext, lsa);
1619	lsa->hdr.ls_chksum = 0;
1620	lsa->hdr.ls_chksum =
1621	    htons(iso_cksum(lsa, len, LS_CKSUM_OFFSET));
1622
1623	return (lsa);
1624}
1625
1626struct lsa *
1627orig_sum_lsa(struct rt_node *rte, struct area *area, u_int8_t type, int invalid)
1628{
1629#if 0 /* XXX a lot todo */
1630	struct lsa	*lsa;
1631	u_int16_t	 len;
1632
1633	len = sizeof(struct lsa_hdr) + sizeof(struct lsa_sum);
1634	if ((lsa = calloc(1, len)) == NULL)
1635		fatal("orig_sum_lsa");
1636
1637	/* LSA header */
1638	lsa->hdr.age = htons(invalid ? MAX_AGE : DEFAULT_AGE);
1639	lsa->hdr.type = type;
1640	lsa->hdr.adv_rtr = rdeconf->rtr_id.s_addr;
1641	lsa->hdr.seq_num = htonl(INIT_SEQ_NUM);
1642	lsa->hdr.len = htons(len);
1643
1644	/* prefix and mask */
1645	/*
1646	 * TODO ls_id must be unique, for overlapping routes this may
1647	 * not be true. In this case a hack needs to be done to
1648	 * make the ls_id unique.
1649	 */
1650	lsa->hdr.ls_id = rte->prefix.s_addr;
1651	if (type == LSA_TYPE_SUM_NETWORK)
1652		lsa->data.sum.mask = prefixlen2mask(rte->prefixlen);
1653	else
1654		lsa->data.sum.mask = 0;	/* must be zero per RFC */
1655
1656	lsa->data.sum.metric = htonl(rte->cost & LSA_METRIC_MASK);
1657
1658	lsa->hdr.ls_chksum = 0;
1659	lsa->hdr.ls_chksum =
1660	    htons(iso_cksum(lsa, len, LS_CKSUM_OFFSET));
1661
1662	return (lsa);
1663#endif
1664	return NULL;
1665}
1666