xref: /freebsd/usr.sbin/rtsold/cap_llflags.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018 The FreeBSD Foundation
5  *
6  * This software was developed by Mark Johnston under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <sys/dnv.h>
34 #include <sys/ioctl.h>
35 #include <sys/nv.h>
36 #include <sys/queue.h>
37 
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <netinet6/in6_var.h>
41 
42 #include <errno.h>
43 #include <ifaddrs.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include <libcasper.h>
48 #include <libcasper_service.h>
49 
50 #include "rtsold.h"
51 
52 /*
53  * A service to fetch the flags for the link-local IPv6 address on the specified
54  * interface.  This cannot easily be done in capability mode because we need to
55  * use the routing socket sysctl API to find the link-local address of a
56  * particular interface.  The SIOCGIFCONF ioctl is one other option, but as
57  * currently implemented it is less flexible (it cannot report the required
58  * buffer length), and hard-codes a buffer length limit.
59  */
60 
61 static int
62 llflags_get(const char *ifname, int *flagsp)
63 {
64 	struct in6_ifreq ifr6;
65 	struct ifaddrs *ifap, *ifa;
66 	struct sockaddr_in6 *sin6;
67 	int error, s;
68 
69 	s = socket(PF_INET6, SOCK_DGRAM, 0);
70 	if (s < 0)
71 		return (-1);
72 
73 	ifap = NULL;
74 	if (getifaddrs(&ifap) != 0) {
75 		error = errno;
76 		goto out;
77 	}
78 	error = ENOENT;
79 	for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
80 		if (strcmp(ifa->ifa_name, ifname) != 0)
81 			continue;
82 		if (ifa->ifa_addr->sa_family != AF_INET6)
83 			continue;
84 
85 		sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr;
86 		if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
87 			continue;
88 
89 		memset(&ifr6, 0, sizeof(ifr6));
90 		if (strlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name)) >=
91 		    sizeof(ifr6.ifr_name)) {
92 			error = EINVAL;
93 			goto out;
94 		}
95 		memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
96 		if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
97 			error = errno;
98 			goto out;
99 		}
100 
101 		*flagsp = ifr6.ifr_ifru.ifru_flags6;
102 		error = 0;
103 		break;
104 	}
105 out:
106 	(void)close(s);
107 	if (ifap != NULL)
108 		freeifaddrs(ifap);
109 	if (error != 0) {
110 		errno = error;
111 		return (-1);
112 	} else {
113 		return (0);
114 	}
115 }
116 
117 int
118 cap_llflags_get(cap_channel_t *cap, const char *ifname, int *flagsp)
119 {
120 #ifdef WITH_CASPER
121 	nvlist_t *nvl;
122 	int error;
123 
124 	nvl = nvlist_create(0);
125 	nvlist_add_string(nvl, "cmd", "get");
126 	nvlist_add_string(nvl, "ifname", ifname);
127 	nvl = cap_xfer_nvlist(cap, nvl);
128 	if (nvl == NULL)
129 		return (-1);
130 	error = (int)dnvlist_get_number(nvl, "error", 0);
131 	if (error == 0)
132 		*flagsp = (int)nvlist_get_number(nvl, "flags");
133 	nvlist_destroy(nvl);
134 	if (error != 0)
135 		errno = error;
136 	return (error == 0 ? 0 : -1);
137 #else
138 	(void)cap;
139 	return (llflags_get(ifname, flagsp));
140 #endif
141 }
142 
143 #ifdef WITH_CASPER
144 static int
145 llflags_command(const char *cmd, const nvlist_t *limits __unused,
146     nvlist_t *nvlin, nvlist_t *nvlout)
147 {
148 	const char *ifname;
149 	int flags;
150 
151 	if (strcmp(cmd, "get") != 0)
152 		return (EINVAL);
153 	ifname = nvlist_get_string(nvlin, "ifname");
154 	if (llflags_get(ifname, &flags) != 0)
155 		return (errno);
156 	nvlist_add_number(nvlout, "flags", flags);
157 	return (0);
158 }
159 
160 CREATE_SERVICE("rtsold.llflags", NULL, llflags_command, 0);
161 #endif /* WITH_CASPER */
162