xref: /openbsd/usr.sbin/rad/printconf.c (revision 5dea098c)
1 /*	$OpenBSD: printconf.c,v 1.8 2024/04/23 22:11:59 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*
43 yesno(int flag)
44 {
45 	return flag ? "yes" : "no";
46 }
47 
48 const char*
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
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 	if (ra_options->mtu > 0)
82 		printf("%smtu %u\n", indent, ra_options->mtu);
83 
84 	if (!SIMPLEQ_EMPTY(&ra_options->ra_rdnss_list) ||
85 	    !SIMPLEQ_EMPTY(&ra_options->ra_dnssl_list)) {
86 		printf("%sdns {\n", indent);
87 		printf("%s\tlifetime %u\n", indent, ra_options->rdns_lifetime);
88 		if (!SIMPLEQ_EMPTY(&ra_options->ra_rdnss_list)) {
89 			printf("%s\tnameserver {\n", indent);
90 			SIMPLEQ_FOREACH(ra_rdnss,
91 			    &ra_options->ra_rdnss_list, entry) {
92 				inet_ntop(AF_INET6, &ra_rdnss->rdnss,
93 				    buf, sizeof(buf));
94 				printf("%s\t\t%s\n", indent, buf);
95 			}
96 			printf("%s\t}\n", indent);
97 		}
98 		if (!SIMPLEQ_EMPTY(&ra_options->ra_dnssl_list)) {
99 			printf("%s\tsearch {\n", indent);
100 			SIMPLEQ_FOREACH(ra_dnssl,
101 			    &ra_options->ra_dnssl_list, entry)
102 				printf("%s\t\t%s\n", indent, ra_dnssl->search);
103 			printf("%s\t}\n", indent);
104 		}
105 		printf("%s}\n", indent);
106 	}
107 	SIMPLEQ_FOREACH(pref64, &ra_options->ra_pref64_list, entry) {
108 		printf("%snat64 prefix %s/%d {\n", indent, inet_ntop(AF_INET6,
109 		    &pref64->prefix, buf, sizeof(buf)), pref64->prefixlen);
110 		printf("%s\tlifetime %u\n", indent, pref64->ltime);
111 		printf("%s}\n", indent);
112 	}
113 
114 }
115 
116 void
117 print_prefix_options(const char *indent, const struct ra_prefix_conf
118     *ra_prefix_conf)
119 {
120 	printf("%svalid lifetime %u\n", indent, ra_prefix_conf->vltime);
121 	printf("%spreferred lifetime %u\n", indent, ra_prefix_conf->pltime);
122 	printf("%son-link %s\n", indent, yesno(ra_prefix_conf->lflag));
123 	printf("%sautonomous address-configuration %s\n", indent,
124 	    yesno(ra_prefix_conf->aflag));
125 }
126 
127 void
128 print_config(struct rad_conf *conf)
129 {
130 	struct ra_iface_conf	*iface;
131 	struct ra_prefix_conf	*prefix;
132 	char			 buf[INET6_ADDRSTRLEN];
133 
134 	print_ra_options("", &conf->ra_options);
135 	printf("\n");
136 
137 	SIMPLEQ_FOREACH(iface, &conf->ra_iface_list, entry) {
138 		printf("interface %s {\n", iface->name);
139 
140 		print_ra_options("\t", &iface->ra_options);
141 
142 		if (iface->autoprefix) {
143 			printf("\tauto prefix {\n");
144 			print_prefix_options("\t\t", iface->autoprefix);
145 			printf("\t}\n");
146 		} else
147 			printf("\tno auto prefix\n");
148 
149 		SIMPLEQ_FOREACH(prefix, &iface->ra_prefix_list, entry) {
150 			printf("\tprefix %s/%d {\n", inet_ntop(AF_INET6,
151 			    &prefix->prefix, buf, sizeof(buf)),
152 			    prefix->prefixlen);
153 			print_prefix_options("\t\t", prefix);
154 			printf("\t}\n");
155 		}
156 
157 		printf("}\n");
158 	}
159 }
160