xref: /openbsd-src/usr.sbin/rad/printconf.c (revision 30ca340796a98c20c16842cbd6e4c25de4709bdb)
1 /*	$OpenBSD: printconf.c,v 1.9 2024/05/17 06:50:14 florian Exp $	*/
2 
3 /*
4  * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
5  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
23 #include <sys/uio.h>
24 
25 #include <netinet/in.h>
26 #include <netinet/icmp6.h>
27 #include <net/if.h>
28 
29 #include <arpa/inet.h>
30 
31 #include <event.h>
32 #include <imsg.h>
33 #include <stdio.h>
34 
35 #include "rad.h"
36 
37 const char*	yesno(int);
38 const char*	rtpref(int);
39 void		print_ra_options(const char*, const struct ra_options_conf*);
40 void		print_prefix_options(const char*, const struct ra_prefix_conf*);
41 
42 const char*
yesno(int flag)43 yesno(int flag)
44 {
45 	return flag ? "yes" : "no";
46 }
47 
48 const char*
rtpref(int rtpref)49 rtpref(int rtpref)
50 {
51 	switch (rtpref & ND_RA_FLAG_RTPREF_MASK) {
52 	case ND_RA_FLAG_RTPREF_HIGH:
53 		return "high";
54 	case ND_RA_FLAG_RTPREF_MEDIUM:
55 		return "medium";
56 	case ND_RA_FLAG_RTPREF_LOW:
57 		return "low";
58 	default:
59 		return "invalid";
60 	}
61 
62 }
63 
64 void
print_ra_options(const char * indent,const struct ra_options_conf * ra_options)65 print_ra_options(const char *indent, const struct ra_options_conf *ra_options)
66 {
67 	struct ra_rdnss_conf	*ra_rdnss;
68 	struct ra_dnssl_conf	*ra_dnssl;
69 	struct ra_pref64_conf	*pref64;
70 	char			 buf[INET6_ADDRSTRLEN];
71 
72 	printf("%sdefault router %s\n", indent, yesno(ra_options->dfr));
73 	printf("%shop limit %d\n", indent, ra_options->cur_hl);
74 	printf("%smanaged address configuration %s\n", indent,
75 	    yesno(ra_options->m_flag));
76 	printf("%sother configuration %s\n", indent, yesno(ra_options->o_flag));
77 	printf("%srouter preference %s\n", indent, rtpref(ra_options->rtpref));
78 	printf("%srouter lifetime %d\n", indent, ra_options->router_lifetime);
79 	printf("%sreachable time %u\n", indent, ra_options->reachable_time);
80 	printf("%sretrans timer %u\n", indent, ra_options->retrans_timer);
81 	printf("%ssource link-layer address %s\n", indent,
82 	    yesno(ra_options->source_link_addr));
83 	if (ra_options->mtu > 0)
84 		printf("%smtu %u\n", indent, ra_options->mtu);
85 
86 	if (!SIMPLEQ_EMPTY(&ra_options->ra_rdnss_list) ||
87 	    !SIMPLEQ_EMPTY(&ra_options->ra_dnssl_list)) {
88 		printf("%sdns {\n", indent);
89 		printf("%s\tlifetime %u\n", indent, ra_options->rdns_lifetime);
90 		if (!SIMPLEQ_EMPTY(&ra_options->ra_rdnss_list)) {
91 			printf("%s\tnameserver {\n", indent);
92 			SIMPLEQ_FOREACH(ra_rdnss,
93 			    &ra_options->ra_rdnss_list, entry) {
94 				inet_ntop(AF_INET6, &ra_rdnss->rdnss,
95 				    buf, sizeof(buf));
96 				printf("%s\t\t%s\n", indent, buf);
97 			}
98 			printf("%s\t}\n", indent);
99 		}
100 		if (!SIMPLEQ_EMPTY(&ra_options->ra_dnssl_list)) {
101 			printf("%s\tsearch {\n", indent);
102 			SIMPLEQ_FOREACH(ra_dnssl,
103 			    &ra_options->ra_dnssl_list, entry)
104 				printf("%s\t\t%s\n", indent, ra_dnssl->search);
105 			printf("%s\t}\n", indent);
106 		}
107 		printf("%s}\n", indent);
108 	}
109 	SIMPLEQ_FOREACH(pref64, &ra_options->ra_pref64_list, entry) {
110 		printf("%snat64 prefix %s/%d {\n", indent, inet_ntop(AF_INET6,
111 		    &pref64->prefix, buf, sizeof(buf)), pref64->prefixlen);
112 		printf("%s\tlifetime %u\n", indent, pref64->ltime);
113 		printf("%s}\n", indent);
114 	}
115 
116 }
117 
118 void
print_prefix_options(const char * indent,const struct ra_prefix_conf * ra_prefix_conf)119 print_prefix_options(const char *indent, const struct ra_prefix_conf
120     *ra_prefix_conf)
121 {
122 	printf("%svalid lifetime %u\n", indent, ra_prefix_conf->vltime);
123 	printf("%spreferred lifetime %u\n", indent, ra_prefix_conf->pltime);
124 	printf("%son-link %s\n", indent, yesno(ra_prefix_conf->lflag));
125 	printf("%sautonomous address-configuration %s\n", indent,
126 	    yesno(ra_prefix_conf->aflag));
127 }
128 
129 void
print_config(struct rad_conf * conf)130 print_config(struct rad_conf *conf)
131 {
132 	struct ra_iface_conf	*iface;
133 	struct ra_prefix_conf	*prefix;
134 	char			 buf[INET6_ADDRSTRLEN];
135 
136 	print_ra_options("", &conf->ra_options);
137 	printf("\n");
138 
139 	SIMPLEQ_FOREACH(iface, &conf->ra_iface_list, entry) {
140 		printf("interface %s {\n", iface->name);
141 
142 		print_ra_options("\t", &iface->ra_options);
143 
144 		if (iface->autoprefix) {
145 			printf("\tauto prefix {\n");
146 			print_prefix_options("\t\t", iface->autoprefix);
147 			printf("\t}\n");
148 		} else
149 			printf("\tno auto prefix\n");
150 
151 		SIMPLEQ_FOREACH(prefix, &iface->ra_prefix_list, entry) {
152 			printf("\tprefix %s/%d {\n", inet_ntop(AF_INET6,
153 			    &prefix->prefix, buf, sizeof(buf)),
154 			    prefix->prefixlen);
155 			print_prefix_options("\t\t", prefix);
156 			printf("\t}\n");
157 		}
158 
159 		printf("}\n");
160 	}
161 }
162