1 /* $KAME: probe.c,v 1.17 2003/10/05 00:09:36 itojun Exp $ */
4 * Copyright (C) 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
40 #include <sys/queue.h>
43 #include <net/if_dl.h>
45 #include <netinet/in.h>
46 #include <netinet6/in6_var.h>
47 #include <netinet/icmp6.h>
48 #include <netinet6/nd6.h>
50 #include <arpa/inet.h>
60 static struct msghdr sndmhdr;
61 static struct iovec sndiov[2];
63 static void sendprobe(struct in6_addr *, struct ifinfo *);
68 int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
69 CMSG_SPACE(sizeof(int));
70 static u_char *sndcmsgbuf = NULL;
72 if (sndcmsgbuf == NULL &&
73 (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) {
74 warnmsg(LOG_ERR, __func__, "malloc failed");
78 if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) {
79 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
83 /* initialize msghdr for sending packets */
84 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
85 sndmhdr.msg_iov = sndiov;
86 sndmhdr.msg_iovlen = 1;
87 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
88 sndmhdr.msg_controllen = scmsglen;
94 * Probe if each router in the default router list is still alive.
97 defrouter_probe(struct ifinfo *ifinfo)
99 struct in6_defrouter *p, *ep;
101 char *buf, ntopbuf[INET6_ADDRSTRLEN];
104 ifindex = ifinfo->sdl->sdl_index;
109 mib[2] = IPPROTO_ICMPV6;
110 mib[3] = ICMPV6CTL_ND6_DRLIST;
111 if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0) {
112 warnmsg(LOG_ERR, __func__, "sysctl(ICMPV6CTL_ND6_DRLIST): %s",
120 warnmsg(LOG_ERR, __func__, "malloc(): %s", strerror(errno));
123 if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) {
124 warnmsg(LOG_ERR, __func__, "sysctl(ICMPV6CTL_ND6_DRLIST): %s",
129 ep = (struct in6_defrouter *)(void *)(buf + l);
130 for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) {
131 if (ifindex != p->if_index)
133 if (!IN6_IS_ADDR_LINKLOCAL(&p->rtaddr.sin6_addr)) {
134 warnmsg(LOG_ERR, __func__,
135 "default router list contains a "
136 "non-link-local address(%s)",
137 inet_ntop(AF_INET6, &p->rtaddr.sin6_addr, ntopbuf,
139 continue; /* ignore the address */
141 sendprobe(&p->rtaddr.sin6_addr, ifinfo);
147 sendprobe(struct in6_addr *addr, struct ifinfo *ifinfo)
149 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
150 struct sockaddr_in6 sa6_probe;
151 struct in6_pktinfo *pi;
153 u_int32_t ifindex = ifinfo->sdl->sdl_index;
156 memset(&sa6_probe, 0, sizeof(sa6_probe));
157 sa6_probe.sin6_family = AF_INET6;
158 sa6_probe.sin6_len = sizeof(sa6_probe);
159 sa6_probe.sin6_addr = *addr;
160 sa6_probe.sin6_scope_id = ifinfo->linkid;
162 sndmhdr.msg_name = (caddr_t)&sa6_probe;
163 sndmhdr.msg_iov[0].iov_base = NULL;
164 sndmhdr.msg_iov[0].iov_len = 0;
166 cm = CMSG_FIRSTHDR(&sndmhdr);
167 /* specify the outgoing interface */
168 cm->cmsg_level = IPPROTO_IPV6;
169 cm->cmsg_type = IPV6_PKTINFO;
170 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
171 pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
172 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
173 pi->ipi6_ifindex = ifindex;
175 /* specify the hop limit of the packet for safety */
176 cm = CMSG_NXTHDR(&sndmhdr, cm);
177 cm->cmsg_level = IPPROTO_IPV6;
178 cm->cmsg_type = IPV6_HOPLIMIT;
179 cm->cmsg_len = CMSG_LEN(sizeof(int));
180 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
182 warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s",
183 inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN),
184 if_indextoname(ifindex, ifnamebuf));
186 if (sendmsg(probesock, &sndmhdr, 0))
187 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
188 if_indextoname(ifindex, ifnamebuf), strerror(errno));