xref: /openbsd-src/usr.sbin/rad/printconf.c (revision 30ca340796a98c20c16842cbd6e4c25de4709bdb)
1*30ca3407Sflorian /*	$OpenBSD: printconf.c,v 1.9 2024/05/17 06:50:14 florian Exp $	*/
253293e44Sflorian 
353293e44Sflorian /*
453293e44Sflorian  * Copyright (c) 2018 Florian Obser <florian@openbsd.org>
553293e44Sflorian  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
653293e44Sflorian  *
753293e44Sflorian  * Permission to use, copy, modify, and distribute this software for any
853293e44Sflorian  * purpose with or without fee is hereby granted, provided that the above
953293e44Sflorian  * copyright notice and this permission notice appear in all copies.
1053293e44Sflorian  *
1153293e44Sflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1253293e44Sflorian  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1353293e44Sflorian  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1453293e44Sflorian  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1553293e44Sflorian  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1653293e44Sflorian  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1753293e44Sflorian  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1853293e44Sflorian  */
1953293e44Sflorian 
2053293e44Sflorian #include <sys/types.h>
2153293e44Sflorian #include <sys/queue.h>
2253293e44Sflorian #include <sys/socket.h>
2353293e44Sflorian #include <sys/uio.h>
2453293e44Sflorian 
2553293e44Sflorian #include <netinet/in.h>
26639a58baSflorian #include <netinet/icmp6.h>
2753293e44Sflorian #include <net/if.h>
2853293e44Sflorian 
2953293e44Sflorian #include <arpa/inet.h>
3053293e44Sflorian 
3153293e44Sflorian #include <event.h>
3253293e44Sflorian #include <imsg.h>
3353293e44Sflorian #include <stdio.h>
3453293e44Sflorian 
3553293e44Sflorian #include "rad.h"
3653293e44Sflorian 
3753293e44Sflorian const char*	yesno(int);
38639a58baSflorian const char*	rtpref(int);
3953293e44Sflorian void		print_ra_options(const char*, const struct ra_options_conf*);
4053293e44Sflorian void		print_prefix_options(const char*, const struct ra_prefix_conf*);
4153293e44Sflorian 
4253293e44Sflorian const char*
yesno(int flag)4353293e44Sflorian yesno(int flag)
4453293e44Sflorian {
4553293e44Sflorian 	return flag ? "yes" : "no";
4653293e44Sflorian }
4753293e44Sflorian 
48639a58baSflorian const char*
rtpref(int rtpref)49639a58baSflorian rtpref(int rtpref)
50639a58baSflorian {
51639a58baSflorian 	switch (rtpref & ND_RA_FLAG_RTPREF_MASK) {
52639a58baSflorian 	case ND_RA_FLAG_RTPREF_HIGH:
53639a58baSflorian 		return "high";
54639a58baSflorian 	case ND_RA_FLAG_RTPREF_MEDIUM:
55639a58baSflorian 		return "medium";
56639a58baSflorian 	case ND_RA_FLAG_RTPREF_LOW:
57639a58baSflorian 		return "low";
58639a58baSflorian 	default:
59639a58baSflorian 		return "invalid";
60639a58baSflorian 	}
61639a58baSflorian 
62639a58baSflorian }
63639a58baSflorian 
6453293e44Sflorian void
print_ra_options(const char * indent,const struct ra_options_conf * ra_options)6553293e44Sflorian print_ra_options(const char *indent, const struct ra_options_conf *ra_options)
6653293e44Sflorian {
678815eebdSflorian 	struct ra_rdnss_conf	*ra_rdnss;
688815eebdSflorian 	struct ra_dnssl_conf	*ra_dnssl;
695207bb19Sflorian 	struct ra_pref64_conf	*pref64;
708815eebdSflorian 	char			 buf[INET6_ADDRSTRLEN];
718815eebdSflorian 
7253293e44Sflorian 	printf("%sdefault router %s\n", indent, yesno(ra_options->dfr));
7353293e44Sflorian 	printf("%shop limit %d\n", indent, ra_options->cur_hl);
7453293e44Sflorian 	printf("%smanaged address configuration %s\n", indent,
7553293e44Sflorian 	    yesno(ra_options->m_flag));
7653293e44Sflorian 	printf("%sother configuration %s\n", indent, yesno(ra_options->o_flag));
77639a58baSflorian 	printf("%srouter preference %s\n", indent, rtpref(ra_options->rtpref));
7853293e44Sflorian 	printf("%srouter lifetime %d\n", indent, ra_options->router_lifetime);
7953293e44Sflorian 	printf("%sreachable time %u\n", indent, ra_options->reachable_time);
8053293e44Sflorian 	printf("%sretrans timer %u\n", indent, ra_options->retrans_timer);
81*30ca3407Sflorian 	printf("%ssource link-layer address %s\n", indent,
82*30ca3407Sflorian 	    yesno(ra_options->source_link_addr));
83cea17583Sbket 	if (ra_options->mtu > 0)
84cea17583Sbket 		printf("%smtu %u\n", indent, ra_options->mtu);
858815eebdSflorian 
868815eebdSflorian 	if (!SIMPLEQ_EMPTY(&ra_options->ra_rdnss_list) ||
878815eebdSflorian 	    !SIMPLEQ_EMPTY(&ra_options->ra_dnssl_list)) {
888815eebdSflorian 		printf("%sdns {\n", indent);
898815eebdSflorian 		printf("%s\tlifetime %u\n", indent, ra_options->rdns_lifetime);
908815eebdSflorian 		if (!SIMPLEQ_EMPTY(&ra_options->ra_rdnss_list)) {
918815eebdSflorian 			printf("%s\tnameserver {\n", indent);
928815eebdSflorian 			SIMPLEQ_FOREACH(ra_rdnss,
938815eebdSflorian 			    &ra_options->ra_rdnss_list, entry) {
948815eebdSflorian 				inet_ntop(AF_INET6, &ra_rdnss->rdnss,
958815eebdSflorian 				    buf, sizeof(buf));
968815eebdSflorian 				printf("%s\t\t%s\n", indent, buf);
978815eebdSflorian 			}
988815eebdSflorian 			printf("%s\t}\n", indent);
998815eebdSflorian 		}
1008815eebdSflorian 		if (!SIMPLEQ_EMPTY(&ra_options->ra_dnssl_list)) {
1018815eebdSflorian 			printf("%s\tsearch {\n", indent);
1028815eebdSflorian 			SIMPLEQ_FOREACH(ra_dnssl,
1038815eebdSflorian 			    &ra_options->ra_dnssl_list, entry)
1048815eebdSflorian 				printf("%s\t\t%s\n", indent, ra_dnssl->search);
1058815eebdSflorian 			printf("%s\t}\n", indent);
1068815eebdSflorian 		}
1078815eebdSflorian 		printf("%s}\n", indent);
1088815eebdSflorian 	}
1095207bb19Sflorian 	SIMPLEQ_FOREACH(pref64, &ra_options->ra_pref64_list, entry) {
1105207bb19Sflorian 		printf("%snat64 prefix %s/%d {\n", indent, inet_ntop(AF_INET6,
1115207bb19Sflorian 		    &pref64->prefix, buf, sizeof(buf)), pref64->prefixlen);
1125207bb19Sflorian 		printf("%s\tlifetime %u\n", indent, pref64->ltime);
1135207bb19Sflorian 		printf("%s}\n", indent);
1145207bb19Sflorian 	}
1155207bb19Sflorian 
11653293e44Sflorian }
11753293e44Sflorian 
11853293e44Sflorian void
print_prefix_options(const char * indent,const struct ra_prefix_conf * ra_prefix_conf)11953293e44Sflorian print_prefix_options(const char *indent, const struct ra_prefix_conf
12053293e44Sflorian     *ra_prefix_conf)
12153293e44Sflorian {
12253293e44Sflorian 	printf("%svalid lifetime %u\n", indent, ra_prefix_conf->vltime);
12353293e44Sflorian 	printf("%spreferred lifetime %u\n", indent, ra_prefix_conf->pltime);
12453293e44Sflorian 	printf("%son-link %s\n", indent, yesno(ra_prefix_conf->lflag));
12553293e44Sflorian 	printf("%sautonomous address-configuration %s\n", indent,
12653293e44Sflorian 	    yesno(ra_prefix_conf->aflag));
12753293e44Sflorian }
12853293e44Sflorian 
12953293e44Sflorian void
print_config(struct rad_conf * conf)13053293e44Sflorian print_config(struct rad_conf *conf)
13153293e44Sflorian {
13253293e44Sflorian 	struct ra_iface_conf	*iface;
13353293e44Sflorian 	struct ra_prefix_conf	*prefix;
1348bc29b82Sflorian 	char			 buf[INET6_ADDRSTRLEN];
13553293e44Sflorian 
13653293e44Sflorian 	print_ra_options("", &conf->ra_options);
13753293e44Sflorian 	printf("\n");
13853293e44Sflorian 
13953293e44Sflorian 	SIMPLEQ_FOREACH(iface, &conf->ra_iface_list, entry) {
14053293e44Sflorian 		printf("interface %s {\n", iface->name);
14153293e44Sflorian 
14253293e44Sflorian 		print_ra_options("\t", &iface->ra_options);
14353293e44Sflorian 
14453293e44Sflorian 		if (iface->autoprefix) {
14553293e44Sflorian 			printf("\tauto prefix {\n");
14653293e44Sflorian 			print_prefix_options("\t\t", iface->autoprefix);
14753293e44Sflorian 			printf("\t}\n");
14853293e44Sflorian 		} else
14953293e44Sflorian 			printf("\tno auto prefix\n");
15053293e44Sflorian 
15153293e44Sflorian 		SIMPLEQ_FOREACH(prefix, &iface->ra_prefix_list, entry) {
1528bc29b82Sflorian 			printf("\tprefix %s/%d {\n", inet_ntop(AF_INET6,
1538bc29b82Sflorian 			    &prefix->prefix, buf, sizeof(buf)),
1548bc29b82Sflorian 			    prefix->prefixlen);
15553293e44Sflorian 			print_prefix_options("\t\t", prefix);
15653293e44Sflorian 			printf("\t}\n");
15753293e44Sflorian 		}
15853293e44Sflorian 
15953293e44Sflorian 		printf("}\n");
16053293e44Sflorian 	}
16153293e44Sflorian }
162