xref: /openbsd-src/usr.sbin/ldpd/printconf.c (revision 7ee91690249e42a5bf33a780035fc47278ffda1a)
1 /*	$OpenBSD: printconf.c,v 1.28 2019/01/23 02:02:04 dlg Exp $ */
2 
3 /*
4  * Copyright (c) 2013, 2016 Renato Westphal <renato@openbsd.org>
5  * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
6  * Copyright (c) 2004, 2005, 2008 Esben Norby <norby@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 <arpa/inet.h>
24 #include <stdio.h>
25 #include <netdb.h>
26 #include <err.h>
27 
28 #include "ldpd.h"
29 #include "ldpe.h"
30 #include "log.h"
31 
32 static void	print_mainconf(struct ldpd_conf *);
33 static void	print_af(int, struct ldpd_conf *, struct ldpd_af_conf *);
34 static void	print_iface(struct iface *, struct iface_af *);
35 static void	print_tnbr(struct tnbr *);
36 static void	print_nbrp(struct nbr_params *);
37 static void	print_l2vpn(struct l2vpn *);
38 static void	print_pw(struct l2vpn_pw *);
39 
40 static void
print_mainconf(struct ldpd_conf * conf)41 print_mainconf(struct ldpd_conf *conf)
42 {
43 	printf("router-id %s\n", inet_ntoa(conf->rtr_id));
44 
45 	if (conf->flags & F_LDPD_NO_FIB_UPDATE)
46 		printf("fib-update no\n");
47 	else
48 		printf("fib-update yes\n");
49 
50 	printf("rdomain %u\n", conf->rdomain);
51 	if (conf->trans_pref == DUAL_STACK_LDPOV4)
52 		printf("transport-preference ipv4\n");
53 	else if (conf->trans_pref == DUAL_STACK_LDPOV6)
54 		printf("transport-preference ipv6\n");
55 
56 	if (conf->flags & F_LDPD_DS_CISCO_INTEROP)
57 		printf("ds-cisco-interop yes\n");
58 	else
59 		printf("ds-cisco-interop no\n");
60 }
61 
62 static void
print_af(int af,struct ldpd_conf * conf,struct ldpd_af_conf * af_conf)63 print_af(int af, struct ldpd_conf *conf, struct ldpd_af_conf *af_conf)
64 {
65 	struct iface		*iface;
66 	struct iface_af		*ia;
67 	struct tnbr		*tnbr;
68 
69 	printf("\naddress-family %s {\n", af_name(af));
70 
71 	if (af_conf->flags & F_LDPD_AF_THELLO_ACCEPT)
72 		printf("\ttargeted-hello-accept yes\n");
73 	else
74 		printf("\ttargeted-hello-accept no\n");
75 
76 	if (af_conf->flags & F_LDPD_AF_EXPNULL)
77 		printf("\texplicit-null yes\n");
78 	else
79 		printf("\texplicit-null no\n");
80 
81 	if (af_conf->flags & F_LDPD_AF_NO_GTSM)
82 		printf("\tgtsm-enable no\n");
83 	else
84 		printf("\tgtsm-enable yes\n");
85 
86 	printf("\tkeepalive %u\n", af_conf->keepalive);
87 	printf("\ttransport-address %s\n", log_addr(af, &af_conf->trans_addr));
88 
89 	LIST_FOREACH(iface, &conf->iface_list, entry) {
90 		ia = iface_af_get(iface, af);
91 		if (ia->enabled)
92 			print_iface(iface, ia);
93 	}
94 
95 	LIST_FOREACH(tnbr, &conf->tnbr_list, entry)
96 		if (tnbr->af == af && tnbr->flags & F_TNBR_CONFIGURED)
97 			print_tnbr(tnbr);
98 
99 	printf("}\n");
100 }
101 
102 static void
print_iface(struct iface * iface,struct iface_af * ia)103 print_iface(struct iface *iface, struct iface_af *ia)
104 {
105 	printf("\tinterface %s {\n", iface->name);
106 	printf("\t\tlink-hello-holdtime %u\n", ia->hello_holdtime);
107 	printf("\t\tlink-hello-interval %u\n", ia->hello_interval);
108 	printf("\t}\n");
109 }
110 
111 static void
print_tnbr(struct tnbr * tnbr)112 print_tnbr(struct tnbr *tnbr)
113 {
114 	printf("\n\ttargeted-neighbor %s {\n", log_addr(tnbr->af, &tnbr->addr));
115 	printf("\t\ttargeted-hello-holdtime %u\n", tnbr->hello_holdtime);
116 	printf("\t\ttargeted-hello-interval %u\n", tnbr->hello_interval);
117 	printf("\t}\n");
118 }
119 
120 static void
print_nbrp(struct nbr_params * nbrp)121 print_nbrp(struct nbr_params *nbrp)
122 {
123 	printf("\nneighbor %s {\n", inet_ntoa(nbrp->lsr_id));
124 
125 	if (nbrp->flags & F_NBRP_KEEPALIVE)
126 		printf("\tkeepalive %u\n", nbrp->keepalive);
127 
128 	if (nbrp->flags & F_NBRP_GTSM) {
129 		if (nbrp->gtsm_enabled)
130 			printf("\tgtsm-enable yes\n");
131 		else
132 			printf("\tgtsm-enable no\n");
133 	}
134 
135 	if (nbrp->flags & F_NBRP_GTSM_HOPS)
136 		printf("\tgtsm-hops %u\n", nbrp->gtsm_hops);
137 
138 	printf("}\n");
139 }
140 
141 static void
print_l2vpn(struct l2vpn * l2vpn)142 print_l2vpn(struct l2vpn *l2vpn)
143 {
144 	struct l2vpn_if	*lif;
145 	struct l2vpn_pw	*pw;
146 
147 	printf("\nl2vpn %s type vpls {\n", l2vpn->name);
148 
149 	if (l2vpn->pw_type == PW_TYPE_ETHERNET)
150 		printf("\tpw-type ethernet\n");
151 	else
152 		printf("\tpw-type ethernet-tagged\n");
153 
154 	printf("\tmtu %u\n", l2vpn->mtu);
155 	if (l2vpn->br_ifindex != 0)
156 		printf("\tbridge %s\n", l2vpn->br_ifname);
157 	LIST_FOREACH(lif, &l2vpn->if_list, entry)
158 		printf("\tinterface %s\n", lif->ifname);
159 	LIST_FOREACH(pw, &l2vpn->pw_list, entry)
160 		print_pw(pw);
161 
162 	printf("}\n");
163 }
164 
165 static void
print_pw(struct l2vpn_pw * pw)166 print_pw(struct l2vpn_pw *pw)
167 {
168 	printf("\tpseudowire %s {\n", pw->ifname);
169 
170 	printf("\t\tneighbor-id %s\n", inet_ntoa(pw->lsr_id));
171 	printf("\t\tneighbor-addr %s\n", log_addr(pw->af, &pw->addr));
172 	printf("\t\tpw-id %u\n", pw->pwid);
173 
174 	if (pw->flags & F_PW_STATUSTLV_CONF)
175 		printf("\t\tstatus-tlv yes\n");
176 	else
177 		printf("\t\tstatus-tlv no\n");
178 
179 	if (pw->flags & F_PW_CWORD_CONF)
180 		printf("\t\tcontrol-word yes\n");
181 	else
182 		printf("\t\tcontrol-word no\n");
183 
184 	printf("\t}\n");
185 }
186 
187 static void
print_auth(struct ldpd_conf * conf)188 print_auth(struct ldpd_conf *conf)
189 {
190 	struct ldp_auth *auth;
191 
192 	printf("\n");
193 
194 	LIST_FOREACH(auth, &conf->auth_list, entry) {
195 		if (auth->md5key_len)
196 			printf("tcp md5sig key XXX");
197 		else
198 			printf("no tcp md5sig");
199 		if (auth->idlen) {
200 			char hbuf[NI_MAXHOST];
201 
202 			if (inet_net_ntop(AF_INET, &auth->id, auth->idlen,
203 			    hbuf, sizeof(hbuf)) == NULL)
204 				err(1, "inet_net_ntop");
205 
206 			printf(" %s", hbuf);
207 		}
208 		printf("\n");
209 	}
210 }
211 
212 void
print_config(struct ldpd_conf * conf)213 print_config(struct ldpd_conf *conf)
214 {
215 	struct nbr_params	*nbrp;
216 	struct l2vpn		*l2vpn;
217 
218 	print_mainconf(conf);
219 
220 	if (!LIST_EMPTY(&conf->auth_list))
221 		print_auth(conf);
222 
223 	if (conf->ipv4.flags & F_LDPD_AF_ENABLED)
224 		print_af(AF_INET, conf, &conf->ipv4);
225 	if (conf->ipv6.flags & F_LDPD_AF_ENABLED)
226 		print_af(AF_INET6, conf, &conf->ipv6);
227 
228 	LIST_FOREACH(nbrp, &conf->nbrp_list, entry)
229 		print_nbrp(nbrp);
230 
231 	LIST_FOREACH(l2vpn, &conf->l2vpn_list, entry)
232 		print_l2vpn(l2vpn);
233 }
234