1 /* $KAME: probe.c,v 1.17 2003/10/05 00:09:36 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (C) 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/param.h>
37 #include <sys/capsicum.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
44 #include <net/if_dl.h>
46 #include <netinet/in.h>
47 #include <netinet6/in6_var.h>
48 #include <netinet/icmp6.h>
49 #include <netinet6/nd6.h>
51 #include <arpa/inet.h>
53 #include <capsicum_helpers.h>
60 #include <libcasper.h>
61 #include <libcasper_service.h>
66 getsocket(int *sockp, int proto)
74 if ((sock = socket(AF_INET6, SOCK_RAW, proto)) < 0)
76 cap_rights_init(&rights, CAP_CONNECT, CAP_SEND);
77 if (caph_rights_limit(sock, &rights) != 0)
85 sendpacket(int sock, struct sockaddr_in6 *dst, uint32_t ifindex, int hoplimit,
86 const void *data, size_t len)
88 uint8_t cmsg[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
89 CMSG_SPACE(sizeof(int))];
92 struct in6_pktinfo *pi;
95 memset(&hdr, 0, sizeof(hdr));
97 hdr.msg_namelen = sizeof(*dst);
100 hdr.msg_control = cmsg;
101 hdr.msg_controllen = sizeof(cmsg);
103 iov.iov_base = __DECONST(void *, data);
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;
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));
122 return (sendmsg(sock, &hdr, 0));
126 probe_defrouters(uint32_t ifindex, uint32_t linkid)
128 static int probesock = -1;
129 struct sockaddr_in6 dst;
130 struct in6_defrouter *p, *ep;
137 if (getsocket(&probesock, IPPROTO_NONE) != 0)
142 mib[2] = IPPROTO_ICMPV6;
143 mib[3] = ICMPV6CTL_ND6_DRLIST;
144 if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
149 memset(&dst, 0, sizeof(dst));
150 dst.sin6_family = AF_INET6;
151 dst.sin6_len = sizeof(dst);
156 if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0)
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)
162 if (!IN6_IS_ADDR_LINKLOCAL(&p->rtaddr.sin6_addr))
164 dst.sin6_addr = p->rtaddr.sin6_addr;
165 dst.sin6_scope_id = linkid;
166 (void)sendpacket(probesock, &dst, ifindex, 1, NULL, 0);
174 rssend(uint32_t ifindex, uint32_t linkid, const void *data, size_t len)
176 static int rssock = -1;
177 struct sockaddr_in6 dst;
180 if (getsocket(&rssock, IPPROTO_ICMPV6) != 0)
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;
189 n = sendpacket(rssock, &dst, ifindex, 255, data, len);
190 if (n < 0 || (size_t)n != len)
196 cap_probe_defrouters(cap_channel_t *cap, struct ifinfo *ifinfo)
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);
207 nvl = cap_xfer_nvlist(cap, nvl);
210 error = (int)dnvlist_get_number(nvl, "error", 0);
213 return (error == 0 ? 0 : -1);
216 return (probe_defrouters(ifinfo->sdl->sdl_index, ifinfo->linkid));
221 cap_rssend(cap_channel_t *cap, struct ifinfo *ifinfo)
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);
232 nvl = cap_xfer_nvlist(cap, nvl);
235 error = (int)dnvlist_get_number(nvl, "error", 0);
240 error = rssend(ifinfo->sdl->sdl_index, ifinfo->linkid, ifinfo->rs_data,
245 if (error != 0 && (errno != ENETDOWN || dflag > 0)) {
247 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
248 ifinfo->ifname, strerror(errno));
251 return (error == 0 ? 0 : -1);
256 sendmsg_command(const char *cmd, const nvlist_t *limits __unused, nvlist_t *nvlin,
257 nvlist_t *nvlout __unused)
261 uint32_t ifindex, linkid;
264 if (strcmp(cmd, "probe_defrouters") != 0 &&
265 strcmp(cmd, "rssend") != 0)
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);
273 data = nvlist_get_binary(nvlin, "data", &len);
274 error = rssend(ifindex, linkid, data, len);
281 CREATE_SERVICE("rtsold.sendmsg", NULL, sendmsg_command, 0);
282 #endif /* WITH_CASPER */