1124524Sume/* $KAME: probe.c,v 1.17 2003/10/05 00:09:36 itojun Exp $ */ 266776Skris 355163Sshin/* 455163Sshin * Copyright (C) 1998 WIDE Project. 555163Sshin * All rights reserved. 662632Skris * 755163Sshin * Redistribution and use in source and binary forms, with or without 855163Sshin * modification, are permitted provided that the following conditions 955163Sshin * are met: 1055163Sshin * 1. Redistributions of source code must retain the above copyright 1155163Sshin * notice, this list of conditions and the following disclaimer. 1255163Sshin * 2. Redistributions in binary form must reproduce the above copyright 1355163Sshin * notice, this list of conditions and the following disclaimer in the 1455163Sshin * documentation and/or other materials provided with the distribution. 1555163Sshin * 3. Neither the name of the project nor the names of its contributors 1655163Sshin * may be used to endorse or promote products derived from this software 1755163Sshin * without specific prior written permission. 1862632Skris * 1955163Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2055163Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2155163Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2255163Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2355163Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2455163Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2555163Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2655163Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2755163Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2855163Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2955163Sshin * SUCH DAMAGE. 3055163Sshin * 3155163Sshin * $FreeBSD: releng/11.0/usr.sbin/rtsold/probe.c 286566 2015-08-10 07:45:13Z ed $ 3255163Sshin */ 3355163Sshin 3455163Sshin#include <sys/param.h> 3555163Sshin#include <sys/types.h> 3655163Sshin#include <sys/ioctl.h> 3755163Sshin#include <sys/socket.h> 38254470Shrs#include <sys/sysctl.h> 3955163Sshin#include <sys/uio.h> 4066776Skris#include <sys/queue.h> 4155163Sshin 4255163Sshin#include <net/if.h> 43119026Sume#include <net/if_dl.h> 4455163Sshin 4555163Sshin#include <netinet/in.h> 4655163Sshin#include <netinet6/in6_var.h> 4755163Sshin#include <netinet/icmp6.h> 4855163Sshin#include <netinet6/nd6.h> 4955163Sshin 5055163Sshin#include <arpa/inet.h> 5155163Sshin 5255163Sshin#include <errno.h> 5355163Sshin#include <unistd.h> 5455163Sshin#include <string.h> 5555163Sshin#include <syslog.h> 5662632Skris#include <stdlib.h> 5755163Sshin 5855163Sshin#include "rtsold.h" 5955163Sshin 6055163Sshinstatic struct msghdr sndmhdr; 6155163Sshinstatic struct iovec sndiov[2]; 6255163Sshinstatic int probesock; 63173412Skevlostatic void sendprobe(struct in6_addr *, struct ifinfo *); 6455163Sshin 6555163Sshinint 66124524Sumeprobe_init(void) 6755163Sshin{ 6862632Skris int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + 69118664Sume CMSG_SPACE(sizeof(int)); 7062632Skris static u_char *sndcmsgbuf = NULL; 71118664Sume 7262632Skris if (sndcmsgbuf == NULL && 7362632Skris (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) { 74118660Sume warnmsg(LOG_ERR, __func__, "malloc failed"); 75222732Shrs return (-1); 7662632Skris } 7755163Sshin 7855163Sshin if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) { 79118660Sume warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno)); 80222732Shrs return (-1); 8155163Sshin } 8255163Sshin 8355163Sshin /* initialize msghdr for sending packets */ 8455163Sshin sndmhdr.msg_namelen = sizeof(struct sockaddr_in6); 8555163Sshin sndmhdr.msg_iov = sndiov; 8655163Sshin sndmhdr.msg_iovlen = 1; 8755163Sshin sndmhdr.msg_control = (caddr_t)sndcmsgbuf; 8862632Skris sndmhdr.msg_controllen = scmsglen; 89222732Shrs 90222732Shrs return (0); 9155163Sshin} 9255163Sshin 9355163Sshin/* 94118664Sume * Probe if each router in the default router list is still alive. 9555163Sshin */ 9655163Sshinvoid 97119026Sumedefrouter_probe(struct ifinfo *ifinfo) 9855163Sshin{ 99254470Shrs struct in6_defrouter *p, *ep; 100254470Shrs int ifindex, mib[4]; 101254470Shrs char *buf, ntopbuf[INET6_ADDRSTRLEN]; 102254470Shrs size_t l; 10355163Sshin 104254470Shrs ifindex = ifinfo->sdl->sdl_index; 105254470Shrs if (ifindex == 0) 10655163Sshin return; 107254470Shrs mib[0] = CTL_NET; 108254470Shrs mib[1] = PF_INET6; 109254470Shrs mib[2] = IPPROTO_ICMPV6; 110254470Shrs mib[3] = ICMPV6CTL_ND6_DRLIST; 111254470Shrs if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0) { 112254470Shrs warnmsg(LOG_ERR, __func__, "sysctl(ICMPV6CTL_ND6_DRLIST): %s", 113254470Shrs strerror(errno)); 114254470Shrs return; 11555163Sshin } 116254470Shrs if (l == 0) 117254470Shrs return; 118254470Shrs buf = malloc(l); 119254470Shrs if (buf == NULL) { 120254470Shrs warnmsg(LOG_ERR, __func__, "malloc(): %s", strerror(errno)); 121254470Shrs return; 122254470Shrs } 123254470Shrs if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) { 124254470Shrs warnmsg(LOG_ERR, __func__, "sysctl(ICMPV6CTL_ND6_DRLIST): %s", 125118664Sume strerror(errno)); 126254470Shrs free(buf); 127254470Shrs return; 12855163Sshin } 129254470Shrs ep = (struct in6_defrouter *)(void *)(buf + l); 130254470Shrs for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) { 131254470Shrs if (ifindex != p->if_index) 132254470Shrs continue; 133254470Shrs if (!IN6_IS_ADDR_LINKLOCAL(&p->rtaddr.sin6_addr)) { 134254470Shrs warnmsg(LOG_ERR, __func__, 135254470Shrs "default router list contains a " 136254470Shrs "non-link-local address(%s)", 137254470Shrs inet_ntop(AF_INET6, &p->rtaddr.sin6_addr, ntopbuf, 138254470Shrs INET6_ADDRSTRLEN)); 139254470Shrs continue; /* ignore the address */ 14055163Sshin } 141254470Shrs sendprobe(&p->rtaddr.sin6_addr, ifinfo); 14255163Sshin } 143254470Shrs free(buf); 14455163Sshin} 14555163Sshin 14655163Sshinstatic void 147119026Sumesendprobe(struct in6_addr *addr, struct ifinfo *ifinfo) 14855163Sshin{ 149119026Sume u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ]; 15055163Sshin struct sockaddr_in6 sa6_probe; 15155163Sshin struct in6_pktinfo *pi; 15255163Sshin struct cmsghdr *cm; 153119026Sume u_int32_t ifindex = ifinfo->sdl->sdl_index; 154118664Sume int hoplimit = 1; 15555163Sshin 156118786Sume memset(&sa6_probe, 0, sizeof(sa6_probe)); 15755163Sshin sa6_probe.sin6_family = AF_INET6; 15855163Sshin sa6_probe.sin6_len = sizeof(sa6_probe); 15955163Sshin sa6_probe.sin6_addr = *addr; 160119026Sume sa6_probe.sin6_scope_id = ifinfo->linkid; 16155163Sshin 16255163Sshin sndmhdr.msg_name = (caddr_t)&sa6_probe; 16355163Sshin sndmhdr.msg_iov[0].iov_base = NULL; 16455163Sshin sndmhdr.msg_iov[0].iov_len = 0; 16555163Sshin 16655163Sshin cm = CMSG_FIRSTHDR(&sndmhdr); 16755163Sshin /* specify the outgoing interface */ 16855163Sshin cm->cmsg_level = IPPROTO_IPV6; 16955163Sshin cm->cmsg_type = IPV6_PKTINFO; 17055163Sshin cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 171254462Shrs pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm); 17255163Sshin memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/ 17355163Sshin pi->ipi6_ifindex = ifindex; 17455163Sshin 17555163Sshin /* specify the hop limit of the packet for safety */ 176118664Sume cm = CMSG_NXTHDR(&sndmhdr, cm); 177118664Sume cm->cmsg_level = IPPROTO_IPV6; 178118664Sume cm->cmsg_type = IPV6_HOPLIMIT; 179118664Sume cm->cmsg_len = CMSG_LEN(sizeof(int)); 180118664Sume memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int)); 18155163Sshin 182118660Sume warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s", 183118664Sume inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN), 184118664Sume if_indextoname(ifindex, ifnamebuf)); 18555163Sshin 18655163Sshin if (sendmsg(probesock, &sndmhdr, 0)) 187118660Sume warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s", 188118664Sume if_indextoname(ifindex, ifnamebuf), strerror(errno)); 18955163Sshin} 190