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