xref: /openbsd/usr.sbin/ospf6d/area.c (revision 3d8817e4)
1 /*	$OpenBSD: area.c,v 1.4 2008/12/28 20:08:31 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2004, 2005, 2007 Esben Norby <norby@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/tree.h>
21 
22 #include <err.h>
23 #include <stdlib.h>
24 
25 #include "ospf6.h"
26 #include "ospf6d.h"
27 #include "ospfe.h"
28 #include "rde.h"
29 #include "log.h"
30 
31 struct area *
32 area_new(void)
33 {
34 	struct area *area = NULL;
35 
36 	if ((area = calloc(1, sizeof(*area))) == NULL)
37 		errx(1, "area_new: calloc");
38 
39 	LIST_INIT(&area->iface_list);
40 	LIST_INIT(&area->nbr_list);
41 	RB_INIT(&area->lsa_tree);
42 
43 	return (area);
44 }
45 
46 int
47 area_del(struct area *area)
48 {
49 	struct iface	*iface = NULL;
50 	struct vertex	*v, *nv;
51 	struct rde_nbr	*n;
52 
53 	/* area is removed so neutralize the demotion done by the area */
54 	if (area->active == 0)
55 		ospfe_demote_area(area, 1);
56 
57 	/* clean lists */
58 	while ((iface = LIST_FIRST(&area->iface_list)) != NULL) {
59 		LIST_REMOVE(iface, entry);
60 		if_del(iface);
61 	}
62 
63 	while ((n = LIST_FIRST(&area->nbr_list)) != NULL)
64 		rde_nbr_del(n);
65 
66 	for (v = RB_MIN(lsa_tree, &area->lsa_tree); v != NULL; v = nv) {
67 		nv = RB_NEXT(lsa_tree, &area->lsa_tree, v);
68 		vertex_free(v);
69 	}
70 
71 	free(area);
72 
73 	return (0);
74 }
75 
76 struct area *
77 area_find(struct ospfd_conf *conf, struct in_addr area_id)
78 {
79 	struct area	*area;
80 
81 	LIST_FOREACH(area, &conf->area_list, entry) {
82 		if (area->id.s_addr == area_id.s_addr) {
83 			return (area);
84 		}
85 	}
86 
87 	return (NULL);
88 }
89 
90 void
91 area_track(struct area *area, int state)
92 {
93 	int	old = area->active;
94 
95 	if (state & NBR_STA_FULL)
96 		area->active++;
97 	else if (area->active == 0)
98 		fatalx("area_track: area already inactive");
99 	else
100 		area->active--;
101 
102 	if (area->active == 0 || old == 0)
103 		ospfe_demote_area(area, old == 0);
104 }
105 
106 int
107 area_border_router(struct ospfd_conf *conf)
108 {
109 	struct area	*area;
110 	int		 active = 0;
111 
112 	LIST_FOREACH(area, &conf->area_list, entry)
113 		if (area->active > 0)
114 			active++;
115 
116 	return (active > 1);
117 }
118 
119 u_int32_t
120 area_ospf_options(struct area *area)
121 {
122 	u_int32_t	opt = OSPF_OPTION_V6 | OSPF_OPTION_R;
123 
124 	if (area && !area->stub)
125 		opt |= OSPF_OPTION_E;
126 
127 	return opt;
128 }
129