xref: /openbsd/usr.sbin/ospfd/printconf.c (revision 771fbea0)
1 /*	$OpenBSD: printconf.c,v 1.22 2020/01/21 20:38:52 remi Exp $ */
2 
3 /*
4  * Copyright (c) 2004, 2005 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/queue.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 
25 #include <stdio.h>
26 
27 #include "ospf.h"
28 #include "ospfd.h"
29 #include "ospfe.h"
30 
31 void	print_mainconf(struct ospfd_conf *);
32 const char *print_no(u_int16_t);
33 void	print_redistribute(struct redist_list *);
34 void	print_rtlabel(struct ospfd_conf *);
35 void	print_iface(struct iface *);
36 
37 void
38 print_mainconf(struct ospfd_conf *conf)
39 {
40 	printf("router-id %s\n", inet_ntoa(conf->rtr_id));
41 
42 	if (conf->flags & OSPFD_FLAG_NO_FIB_UPDATE)
43 		printf("fib-update no\n");
44 	else
45 		printf("fib-update yes\n");
46 
47 	printf("fib-priority %hhu\n", conf->fib_priority);
48 
49 	if (conf->rdomain)
50 		printf("rdomain %d\n", conf->rdomain);
51 
52 	if (conf->rfc1583compat)
53 		printf("rfc1583compat yes\n");
54 	else
55 		printf("rfc1583compat no\n");
56 
57 	if (conf->flags & OSPFD_FLAG_STUB_ROUTER)
58 		printf("stub router yes\n");
59 
60 	print_redistribute(&conf->redist_list);
61 	print_rtlabel(conf);
62 
63 	printf("spf-delay msec %u\n", conf->spf_delay);
64 	printf("spf-holdtime msec %u\n", conf->spf_hold_time);
65 }
66 
67 const char *
68 print_no(u_int16_t type)
69 {
70 	if (type & REDIST_NO)
71 		return ("no ");
72 	else
73 		return ("");
74 }
75 
76 void
77 print_redistribute(struct redist_list *rlh)
78 {
79 	struct redistribute	*r;
80 
81 	SIMPLEQ_FOREACH(r, rlh, entry) {
82 		switch (r->type & ~REDIST_NO) {
83 		case REDIST_STATIC:
84 			printf("%sredistribute static ", print_no(r->type));
85 			break;
86 		case REDIST_CONNECTED:
87 			printf("%sredistribute connected ", print_no(r->type));
88 			break;
89 		case REDIST_LABEL:
90 			printf("%sredistribute rtlabel %s ",
91 			    print_no(r->type), rtlabel_id2name(r->label));
92 			break;
93 		case REDIST_ADDR:
94 			printf("%sredistribute %s/%d ",
95 			    print_no(r->type), inet_ntoa(r->addr),
96 			    mask2prefixlen(r->mask.s_addr));
97 			break;
98 		case REDIST_DEFAULT:
99 			printf("%sredistribute default ", print_no(r->type));
100 			break;
101 		}
102 		printf("set { metric %d type %d }",
103 		    (r->metric & LSA_METRIC_MASK),
104 		    ((r->metric & LSA_ASEXT_E_FLAG) == 0 ? 1 : 2));
105 		if (r->dependon[0])
106 			printf(" depend on %s", r->dependon);
107 		printf("\n");
108 	}
109 }
110 
111 void
112 print_rtlabel(struct ospfd_conf *conf)
113 {
114 	struct n2id_label	*label;
115 
116 	TAILQ_FOREACH(label, &rt_labels, entry)
117 		if (label->ext_tag)
118 			printf("rtlabel \"%s\" external-tag %u\n",
119 			    label->name, label->ext_tag);
120 }
121 
122 void
123 print_iface(struct iface *iface)
124 {
125 	struct auth_md	*md;
126 
127 	printf("\tinterface %s:%s {\n", iface->name, inet_ntoa(iface->addr));
128 
129 	printf("\t\tmetric %d\n", iface->metric);
130 
131 	if (iface->demote_group[0] != '\0')
132 		printf("\t\tdemote %s\n", iface->demote_group);
133 	if (iface->dependon[0] != '\0')
134 		printf("\t\tdepend on %s\n", iface->dependon);
135 	if (iface->passive)
136 		printf("\t\tpassive\n");
137 	else {
138 		printf("\t\tretransmit-interval %d\n", iface->rxmt_interval);
139 		if (iface->dead_interval == FAST_RTR_DEAD_TIME) {
140 			printf("\t\trouter-dead-time minimal\n");
141 			printf("\t\tfast-hello-interval msec %u\n",
142 			    iface->fast_hello_interval);
143 		} else {
144 			printf("\t\trouter-dead-time %d\n",
145 			    iface->dead_interval);
146 			printf("\t\thello-interval %d\n",
147 			    iface->hello_interval);
148 		}
149 		printf("\t\trouter-priority %d\n", iface->priority);
150 		printf("\t\ttransmit-delay %d\n", iface->transmit_delay);
151 
152 		if (iface->type == IF_TYPE_POINTOPOINT)
153 			printf("\t\ttype p2p\n");
154 
155 		printf("\t\tauth-type %s\n", if_auth_name(iface->auth_type));
156 		switch (iface->auth_type) {
157 		case AUTH_TYPE_NONE:
158 			break;
159 		case AUTH_TYPE_SIMPLE:
160 			printf("\t\tauth-key XXXXXX\n");
161 			break;
162 		case AUTH_TYPE_CRYPT:
163 			printf("\t\tauth-md-keyid %d\n", iface->auth_keyid);
164 			TAILQ_FOREACH(md, &iface->auth_md_list, entry)
165 				printf("\t\tauth-md %d XXXXXX\n", md->keyid);
166 			break;
167 		default:
168 			printf("\t\tunknown auth type!\n");
169 			break;
170 		}
171 	}
172 
173 	printf("\t}\n");
174 }
175 
176 void
177 print_config(struct ospfd_conf *conf)
178 {
179 	struct area	*area;
180 	struct iface	*iface;
181 
182 	printf("\n");
183 	print_mainconf(conf);
184 	printf("\n");
185 
186 	LIST_FOREACH(area, &conf->area_list, entry) {
187 		printf("area %s {\n", inet_ntoa(area->id));
188 		if (area->stub) {
189 			printf("\tstub");
190 			if (SIMPLEQ_EMPTY(&area->redist_list))
191 				printf("\n");
192 			else {
193 				printf(" ");
194 				print_redistribute(&area->redist_list);
195 			}
196 		}
197 		if (*area->demote_group)
198 			printf("\tdemote %s %d\n", area->demote_group,
199 			area->demote_level);
200 		LIST_FOREACH(iface, &area->iface_list, entry) {
201 			print_iface(iface);
202 		}
203 		printf("}\n\n");
204 	}
205 }
206