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 * $FreeBSD$
34 */
35
36#include <sys/param.h>
37#include <sys/capsicum.h>
38#include <sys/dnv.h>
39#include <sys/nv.h>
40#include <sys/socket.h>
41#include <sys/sysctl.h>
42
43#include <net/if.h>
44#include <net/if_dl.h>
45
46#include <netinet/in.h>
47#include <netinet6/in6_var.h>
48#include <netinet/icmp6.h>
49#include <netinet6/nd6.h>
50
51#include <arpa/inet.h>
52
53#include <capsicum_helpers.h>
54#include <errno.h>
55#include <stdlib.h>
56#include <string.h>
57#include <syslog.h>
58#include <unistd.h>
59
60#include <libcasper.h>
61#include <libcasper_service.h>
62
63#include "rtsold.h"
64
65static int
66getsocket(int *sockp, int proto)
67{
68	cap_rights_t rights;
69	int sock;
70
71	if (*sockp >= 0)
72		return (0);
73
74	if ((sock = socket(AF_INET6, SOCK_RAW, proto)) < 0)
75		return (-1);
76	cap_rights_init(&rights, CAP_CONNECT, CAP_SEND);
77	if (caph_rights_limit(sock, &rights) != 0)
78		return (-1);
79	*sockp = sock;
80
81	return (0);
82}
83
84static ssize_t
85sendpacket(int sock, struct sockaddr_in6 *dst, uint32_t ifindex, int hoplimit,
86    const void *data, size_t len)
87{
88	uint8_t cmsg[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
89	    CMSG_SPACE(sizeof(int))];
90	struct msghdr hdr;
91	struct iovec iov;
92	struct in6_pktinfo *pi;
93	struct cmsghdr *cm;
94
95	memset(&hdr, 0, sizeof(hdr));
96	hdr.msg_name = dst;
97	hdr.msg_namelen = sizeof(*dst);
98	hdr.msg_iov = &iov;
99	hdr.msg_iovlen = 1;
100	hdr.msg_control = cmsg;
101	hdr.msg_controllen = sizeof(cmsg);
102
103	iov.iov_base = __DECONST(void *, data);
104	iov.iov_len = len;
105
106	/* Specify the outbound interface. */
107	cm = CMSG_FIRSTHDR(&hdr);
108	cm->cmsg_level = IPPROTO_IPV6;
109	cm->cmsg_type = IPV6_PKTINFO;
110	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
111	pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
112	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));	/*XXX*/
113	pi->ipi6_ifindex = ifindex;
114
115	/* Specify the hop limit of the packet for safety. */
116	cm = CMSG_NXTHDR(&hdr, cm);
117	cm->cmsg_level = IPPROTO_IPV6;
118	cm->cmsg_type = IPV6_HOPLIMIT;
119	cm->cmsg_len = CMSG_LEN(sizeof(int));
120	memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
121
122	return (sendmsg(sock, &hdr, 0));
123}
124
125static int
126probe_defrouters(uint32_t ifindex, uint32_t linkid)
127{
128	static int probesock = -1;
129	struct sockaddr_in6 dst;
130	struct in6_defrouter *p, *ep;
131	char *buf;
132	size_t len;
133	int mib[4];
134
135	if (ifindex == 0)
136		return (0);
137	if (getsocket(&probesock, IPPROTO_NONE) != 0)
138		return (-1);
139
140	mib[0] = CTL_NET;
141	mib[1] = PF_INET6;
142	mib[2] = IPPROTO_ICMPV6;
143	mib[3] = ICMPV6CTL_ND6_DRLIST;
144	if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
145		return (-1);
146	if (len == 0)
147		return (0);
148
149	memset(&dst, 0, sizeof(dst));
150	dst.sin6_family = AF_INET6;
151	dst.sin6_len = sizeof(dst);
152
153	buf = malloc(len);
154	if (buf == NULL)
155		return (-1);
156	if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0)
157		return (-1);
158	ep = (struct in6_defrouter *)(void *)(buf + len);
159	for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) {
160		if (ifindex != p->if_index)
161			continue;
162		if (!IN6_IS_ADDR_LINKLOCAL(&p->rtaddr.sin6_addr))
163			continue;
164		dst.sin6_addr = p->rtaddr.sin6_addr;
165		dst.sin6_scope_id = linkid;
166		(void)sendpacket(probesock, &dst, ifindex, 1, NULL, 0);
167	}
168	free(buf);
169
170	return (0);
171}
172
173static int
174rssend(uint32_t ifindex, uint32_t linkid, const void *data, size_t len)
175{
176	static int rssock = -1;
177	struct sockaddr_in6 dst;
178	ssize_t n;
179
180	if (getsocket(&rssock, IPPROTO_ICMPV6) != 0)
181		return (-1);
182
183	memset(&dst, 0, sizeof(dst));
184	dst.sin6_family = AF_INET6;
185	dst.sin6_len = sizeof(dst);
186	dst.sin6_addr = (struct in6_addr)IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
187	dst.sin6_scope_id = linkid;
188
189	n = sendpacket(rssock, &dst, ifindex, 255, data, len);
190	if (n < 0 || (size_t)n != len)
191		return (-1);
192	return (0);
193}
194
195int
196cap_probe_defrouters(cap_channel_t *cap, struct ifinfo *ifinfo)
197{
198#ifdef WITH_CASPER
199	nvlist_t *nvl;
200	int error;
201
202	nvl = nvlist_create(0);
203	nvlist_add_string(nvl, "cmd", "probe_defrouters");
204	nvlist_add_number(nvl, "ifindex", ifinfo->sdl->sdl_index);
205	nvlist_add_number(nvl, "linkid", ifinfo->linkid);
206
207	nvl = cap_xfer_nvlist(cap, nvl);
208	if (nvl == NULL)
209		return (errno);
210	error = (int)dnvlist_get_number(nvl, "error", 0);
211	nvlist_destroy(nvl);
212	errno = error;
213	return (error == 0 ? 0 : -1);
214#else
215	(void)cap;
216	return (probe_defrouters(ifinfo->sdl->sdl_index, ifinfo->linkid));
217#endif
218}
219
220int
221cap_rssend(cap_channel_t *cap, struct ifinfo *ifinfo)
222{
223	int error;
224
225#ifdef WITH_CASPER
226	nvlist_t *nvl = nvlist_create(0);
227	nvlist_add_string(nvl, "cmd", "rssend");
228	nvlist_add_number(nvl, "ifindex", ifinfo->sdl->sdl_index);
229	nvlist_add_number(nvl, "linkid", ifinfo->linkid);
230	nvlist_add_binary(nvl, "data", ifinfo->rs_data, ifinfo->rs_datalen);
231
232	nvl = cap_xfer_nvlist(cap, nvl);
233	if (nvl == NULL)
234		return (errno);
235	error = (int)dnvlist_get_number(nvl, "error", 0);
236	nvlist_destroy(nvl);
237	errno = error;
238#else
239	(void)cap;
240	error = rssend(ifinfo->sdl->sdl_index, ifinfo->linkid, ifinfo->rs_data,
241	    ifinfo->rs_datalen);
242#endif
243
244	ifinfo->probes++;
245	if (error != 0 && (errno != ENETDOWN || dflag > 0)) {
246		error = errno;
247		warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
248		    ifinfo->ifname, strerror(errno));
249		errno = error;
250	}
251	return (error == 0 ? 0 : -1);
252}
253
254#ifdef WITH_CASPER
255static int
256sendmsg_command(const char *cmd, const nvlist_t *limits __unused, nvlist_t *nvlin,
257    nvlist_t *nvlout __unused)
258{
259	const void *data;
260	size_t len;
261	uint32_t ifindex, linkid;
262	int error;
263
264	if (strcmp(cmd, "probe_defrouters") != 0 &&
265	    strcmp(cmd, "rssend") != 0)
266		return (EINVAL);
267
268	ifindex = (uint32_t)nvlist_get_number(nvlin, "ifindex");
269	linkid = (uint32_t)nvlist_get_number(nvlin, "linkid");
270	if (strcmp(cmd, "probe_defrouters") == 0) {
271		error = probe_defrouters(ifindex, linkid);
272	} else {
273		data = nvlist_get_binary(nvlin, "data", &len);
274		error = rssend(ifindex, linkid, data, len);
275	}
276	if (error != 0)
277		return (errno);
278	return (0);
279}
280
281CREATE_SERVICE("rtsold.sendmsg", NULL, sendmsg_command, 0);
282#endif /* WITH_CASPER */
283