xref: /freebsd/usr.sbin/rtsold/cap_sendmsg.c (revision 61e21613)
1 /*	$KAME: probe.c,v 1.17 2003/10/05 00:09:36 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1998 WIDE Project.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are 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 the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/capsicum.h>
36 #include <sys/dnv.h>
37 #include <sys/nv.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
40 
41 #include <net/if.h>
42 #include <net/if_dl.h>
43 
44 #include <netinet/in.h>
45 #include <netinet6/in6_var.h>
46 #include <netinet/icmp6.h>
47 #include <netinet6/nd6.h>
48 
49 #include <arpa/inet.h>
50 
51 #include <capsicum_helpers.h>
52 #include <errno.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <syslog.h>
56 #include <unistd.h>
57 
58 #include <libcasper.h>
59 #include <libcasper_service.h>
60 
61 #include "rtsold.h"
62 
63 static int
64 getsocket(int *sockp, int proto)
65 {
66 	cap_rights_t rights;
67 	int sock;
68 
69 	if (*sockp >= 0)
70 		return (0);
71 
72 	if ((sock = socket(AF_INET6, SOCK_RAW, proto)) < 0)
73 		return (-1);
74 	cap_rights_init(&rights, CAP_CONNECT, CAP_SEND);
75 	if (caph_rights_limit(sock, &rights) != 0)
76 		return (-1);
77 	*sockp = sock;
78 
79 	return (0);
80 }
81 
82 static ssize_t
83 sendpacket(int sock, struct sockaddr_in6 *dst, uint32_t ifindex, int hoplimit,
84     const void *data, size_t len)
85 {
86 	uint8_t cmsg[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
87 	    CMSG_SPACE(sizeof(int))];
88 	struct msghdr hdr;
89 	struct iovec iov;
90 	struct in6_pktinfo *pi;
91 	struct cmsghdr *cm;
92 
93 	memset(&hdr, 0, sizeof(hdr));
94 	hdr.msg_name = dst;
95 	hdr.msg_namelen = sizeof(*dst);
96 	hdr.msg_iov = &iov;
97 	hdr.msg_iovlen = 1;
98 	hdr.msg_control = cmsg;
99 	hdr.msg_controllen = sizeof(cmsg);
100 
101 	iov.iov_base = __DECONST(void *, data);
102 	iov.iov_len = len;
103 
104 	/* Specify the outbound interface. */
105 	cm = CMSG_FIRSTHDR(&hdr);
106 	cm->cmsg_level = IPPROTO_IPV6;
107 	cm->cmsg_type = IPV6_PKTINFO;
108 	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
109 	pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
110 	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));	/*XXX*/
111 	pi->ipi6_ifindex = ifindex;
112 
113 	/* Specify the hop limit of the packet for safety. */
114 	cm = CMSG_NXTHDR(&hdr, cm);
115 	cm->cmsg_level = IPPROTO_IPV6;
116 	cm->cmsg_type = IPV6_HOPLIMIT;
117 	cm->cmsg_len = CMSG_LEN(sizeof(int));
118 	memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
119 
120 	return (sendmsg(sock, &hdr, 0));
121 }
122 
123 static int
124 probe_defrouters(uint32_t ifindex, uint32_t linkid)
125 {
126 	static int probesock = -1;
127 	struct sockaddr_in6 dst;
128 	struct in6_defrouter *p, *ep;
129 	char *buf;
130 	size_t len;
131 	int mib[4];
132 
133 	if (ifindex == 0)
134 		return (0);
135 	if (getsocket(&probesock, IPPROTO_NONE) != 0)
136 		return (-1);
137 
138 	mib[0] = CTL_NET;
139 	mib[1] = PF_INET6;
140 	mib[2] = IPPROTO_ICMPV6;
141 	mib[3] = ICMPV6CTL_ND6_DRLIST;
142 	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
143 		return (-1);
144 	if (len == 0)
145 		return (0);
146 
147 	memset(&dst, 0, sizeof(dst));
148 	dst.sin6_family = AF_INET6;
149 	dst.sin6_len = sizeof(dst);
150 
151 	buf = malloc(len);
152 	if (buf == NULL)
153 		return (-1);
154 	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0)
155 		return (-1);
156 	ep = (struct in6_defrouter *)(void *)(buf + len);
157 	for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) {
158 		if (ifindex != p->if_index)
159 			continue;
160 		if (!IN6_IS_ADDR_LINKLOCAL(&p->rtaddr.sin6_addr))
161 			continue;
162 		dst.sin6_addr = p->rtaddr.sin6_addr;
163 		dst.sin6_scope_id = linkid;
164 		(void)sendpacket(probesock, &dst, ifindex, 1, NULL, 0);
165 	}
166 	free(buf);
167 
168 	return (0);
169 }
170 
171 static int
172 rssend(uint32_t ifindex, uint32_t linkid, const void *data, size_t len)
173 {
174 	static int rssock = -1;
175 	struct sockaddr_in6 dst;
176 	ssize_t n;
177 
178 	if (getsocket(&rssock, IPPROTO_ICMPV6) != 0)
179 		return (-1);
180 
181 	memset(&dst, 0, sizeof(dst));
182 	dst.sin6_family = AF_INET6;
183 	dst.sin6_len = sizeof(dst);
184 	dst.sin6_addr = (struct in6_addr)IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
185 	dst.sin6_scope_id = linkid;
186 
187 	n = sendpacket(rssock, &dst, ifindex, 255, data, len);
188 	if (n < 0 || (size_t)n != len)
189 		return (-1);
190 	return (0);
191 }
192 
193 int
194 cap_probe_defrouters(cap_channel_t *cap, struct ifinfo *ifinfo)
195 {
196 #ifdef WITH_CASPER
197 	nvlist_t *nvl;
198 	int error;
199 
200 	nvl = nvlist_create(0);
201 	nvlist_add_string(nvl, "cmd", "probe_defrouters");
202 	nvlist_add_number(nvl, "ifindex", ifinfo->sdl->sdl_index);
203 	nvlist_add_number(nvl, "linkid", ifinfo->linkid);
204 
205 	nvl = cap_xfer_nvlist(cap, nvl);
206 	if (nvl == NULL)
207 		return (errno);
208 	error = (int)dnvlist_get_number(nvl, "error", 0);
209 	nvlist_destroy(nvl);
210 	errno = error;
211 	return (error == 0 ? 0 : -1);
212 #else
213 	(void)cap;
214 	return (probe_defrouters(ifinfo->sdl->sdl_index, ifinfo->linkid));
215 #endif
216 }
217 
218 int
219 cap_rssend(cap_channel_t *cap, struct ifinfo *ifinfo)
220 {
221 	int error;
222 
223 #ifdef WITH_CASPER
224 	nvlist_t *nvl = nvlist_create(0);
225 	nvlist_add_string(nvl, "cmd", "rssend");
226 	nvlist_add_number(nvl, "ifindex", ifinfo->sdl->sdl_index);
227 	nvlist_add_number(nvl, "linkid", ifinfo->linkid);
228 	nvlist_add_binary(nvl, "data", ifinfo->rs_data, ifinfo->rs_datalen);
229 
230 	nvl = cap_xfer_nvlist(cap, nvl);
231 	if (nvl == NULL)
232 		return (errno);
233 	error = (int)dnvlist_get_number(nvl, "error", 0);
234 	nvlist_destroy(nvl);
235 	errno = error;
236 #else
237 	(void)cap;
238 	error = rssend(ifinfo->sdl->sdl_index, ifinfo->linkid, ifinfo->rs_data,
239 	    ifinfo->rs_datalen);
240 #endif
241 
242 	ifinfo->probes++;
243 	if (error != 0 && (errno != ENETDOWN || dflag > 0)) {
244 		error = errno;
245 		warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
246 		    ifinfo->ifname, strerror(errno));
247 		errno = error;
248 	}
249 	return (error == 0 ? 0 : -1);
250 }
251 
252 #ifdef WITH_CASPER
253 static int
254 sendmsg_command(const char *cmd, const nvlist_t *limits __unused, nvlist_t *nvlin,
255     nvlist_t *nvlout __unused)
256 {
257 	const void *data;
258 	size_t len;
259 	uint32_t ifindex, linkid;
260 	int error;
261 
262 	if (strcmp(cmd, "probe_defrouters") != 0 &&
263 	    strcmp(cmd, "rssend") != 0)
264 		return (EINVAL);
265 
266 	ifindex = (uint32_t)nvlist_get_number(nvlin, "ifindex");
267 	linkid = (uint32_t)nvlist_get_number(nvlin, "linkid");
268 	if (strcmp(cmd, "probe_defrouters") == 0) {
269 		error = probe_defrouters(ifindex, linkid);
270 	} else {
271 		data = nvlist_get_binary(nvlin, "data", &len);
272 		error = rssend(ifindex, linkid, data, len);
273 	}
274 	if (error != 0)
275 		return (errno);
276 	return (0);
277 }
278 
279 CREATE_SERVICE("rtsold.sendmsg", NULL, sendmsg_command, 0);
280 #endif /* WITH_CASPER */
281