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
34 #include <sys/param.h>
35 #include <sys/capsicum.h>
38 #include <sys/socket.h>
39 #include <sys/sysctl.h>
42 #include <net/if_dl.h>
44 #include <netinet/in.h>
45 #include <netinet6/in6_var.h>
46 #include <netinet/icmp6.h>
47 #include <netinet6/nd6.h>
49 #include <arpa/inet.h>
51 #include <capsicum_helpers.h>
58 #include <libcasper.h>
59 #include <libcasper_service.h>
64 getsocket(int *sockp, int proto)
72 if ((sock = socket(AF_INET6, SOCK_RAW, proto)) < 0)
74 cap_rights_init(&rights, CAP_CONNECT, CAP_SEND);
75 if (caph_rights_limit(sock, &rights) != 0)
83 sendpacket(int sock, struct sockaddr_in6 *dst, uint32_t ifindex, int hoplimit,
84 const void *data, size_t len)
86 uint8_t cmsg[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
87 CMSG_SPACE(sizeof(int))];
90 struct in6_pktinfo *pi;
93 memset(&hdr, 0, sizeof(hdr));
95 hdr.msg_namelen = sizeof(*dst);
98 hdr.msg_control = cmsg;
99 hdr.msg_controllen = sizeof(cmsg);
101 iov.iov_base = __DECONST(void *, data);
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;
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));
120 return (sendmsg(sock, &hdr, 0));
124 probe_defrouters(uint32_t ifindex, uint32_t linkid)
126 static int probesock = -1;
127 struct sockaddr_in6 dst;
128 struct in6_defrouter *p, *ep;
135 if (getsocket(&probesock, IPPROTO_NONE) != 0)
140 mib[2] = IPPROTO_ICMPV6;
141 mib[3] = ICMPV6CTL_ND6_DRLIST;
142 if (sysctl(mib, nitems(mib), NULL, &len, NULL, 0) < 0)
147 memset(&dst, 0, sizeof(dst));
148 dst.sin6_family = AF_INET6;
149 dst.sin6_len = sizeof(dst);
154 if (sysctl(mib, nitems(mib), buf, &len, NULL, 0) < 0)
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)
160 if (!IN6_IS_ADDR_LINKLOCAL(&p->rtaddr.sin6_addr))
162 dst.sin6_addr = p->rtaddr.sin6_addr;
163 dst.sin6_scope_id = linkid;
164 (void)sendpacket(probesock, &dst, ifindex, 1, NULL, 0);
172 rssend(uint32_t ifindex, uint32_t linkid, const void *data, size_t len)
174 static int rssock = -1;
175 struct sockaddr_in6 dst;
178 if (getsocket(&rssock, IPPROTO_ICMPV6) != 0)
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;
187 n = sendpacket(rssock, &dst, ifindex, 255, data, len);
188 if (n < 0 || (size_t)n != len)
194 cap_probe_defrouters(cap_channel_t *cap, struct ifinfo *ifinfo)
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);
205 nvl = cap_xfer_nvlist(cap, nvl);
208 error = (int)dnvlist_get_number(nvl, "error", 0);
211 return (error == 0 ? 0 : -1);
214 return (probe_defrouters(ifinfo->sdl->sdl_index, ifinfo->linkid));
219 cap_rssend(cap_channel_t *cap, struct ifinfo *ifinfo)
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);
230 nvl = cap_xfer_nvlist(cap, nvl);
233 error = (int)dnvlist_get_number(nvl, "error", 0);
238 error = rssend(ifinfo->sdl->sdl_index, ifinfo->linkid, ifinfo->rs_data,
243 if (error != 0 && (errno != ENETDOWN || dflag > 0)) {
245 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
246 ifinfo->ifname, strerror(errno));
249 return (error == 0 ? 0 : -1);
254 sendmsg_command(const char *cmd, const nvlist_t *limits __unused, nvlist_t *nvlin,
255 nvlist_t *nvlout __unused)
259 uint32_t ifindex, linkid;
262 if (strcmp(cmd, "probe_defrouters") != 0 &&
263 strcmp(cmd, "rssend") != 0)
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);
271 data = nvlist_get_binary(nvlin, "data", &len);
272 error = rssend(ifindex, linkid, data, len);
279 CREATE_SERVICE("rtsold.sendmsg", NULL, sendmsg_command, 0);
280 #endif /* WITH_CASPER */