probe.c revision 118664
178064Sume/* $KAME: probe.c,v 1.10 2000/08/13 06:14:59 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: head/usr.sbin/rtsold/probe.c 118664 2003-08-08 16:56:01Z ume $ 3255163Sshin */ 3355163Sshin 3455163Sshin#include <sys/param.h> 3555163Sshin#include <sys/types.h> 3655163Sshin#include <sys/ioctl.h> 3755163Sshin#include <sys/socket.h> 3855163Sshin#include <sys/uio.h> 3966776Skris#include <sys/queue.h> 4055163Sshin 4155163Sshin#include <net/if.h> 4262632Skris#if defined(__FreeBSD__) && __FreeBSD__ >= 3 4355163Sshin#include <net/if_var.h> 4462632Skris#endif /* __FreeBSD__ >= 3 */ 4555163Sshin 4655163Sshin#include <netinet/in.h> 4755163Sshin#include <netinet6/in6_var.h> 4855163Sshin#include <netinet/icmp6.h> 4955163Sshin#include <netinet6/nd6.h> 5055163Sshin 5155163Sshin#include <arpa/inet.h> 5255163Sshin 5355163Sshin#include <errno.h> 5455163Sshin#include <unistd.h> 5555163Sshin#include <string.h> 5655163Sshin#include <syslog.h> 5762632Skris#include <stdlib.h> 5855163Sshin 5955163Sshin#include "rtsold.h" 6055163Sshin 6155163Sshinstatic struct msghdr sndmhdr; 6255163Sshinstatic struct iovec sndiov[2]; 6355163Sshinstatic int probesock; 64118664Sumestatic void sendprobe __P((struct in6_addr *, int)); 6555163Sshin 6655163Sshinint 6755163Sshinprobe_init() 6855163Sshin{ 6962632Skris int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + 70118664Sume CMSG_SPACE(sizeof(int)); 7162632Skris static u_char *sndcmsgbuf = NULL; 72118664Sume 7362632Skris if (sndcmsgbuf == NULL && 7462632Skris (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) { 75118660Sume warnmsg(LOG_ERR, __func__, "malloc failed"); 7662632Skris return(-1); 7762632Skris } 7855163Sshin 7955163Sshin if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) { 80118660Sume warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno)); 8155163Sshin return(-1); 8255163Sshin } 8355163Sshin 8455163Sshin /* make the socket send-only */ 8555163Sshin if (shutdown(probesock, 0)) { 86118660Sume warnmsg(LOG_ERR, __func__, "shutdown: %s", strerror(errno)); 8755163Sshin return(-1); 8855163Sshin } 8955163Sshin 9055163Sshin /* initialize msghdr for sending packets */ 9155163Sshin sndmhdr.msg_namelen = sizeof(struct sockaddr_in6); 9255163Sshin sndmhdr.msg_iov = sndiov; 9355163Sshin sndmhdr.msg_iovlen = 1; 9455163Sshin sndmhdr.msg_control = (caddr_t)sndcmsgbuf; 9562632Skris sndmhdr.msg_controllen = scmsglen; 9655163Sshin return(0); 9755163Sshin} 9855163Sshin 9955163Sshin/* 100118664Sume * Probe if each router in the default router list is still alive. 10155163Sshin */ 10255163Sshinvoid 10355163Sshindefrouter_probe(int ifindex) 10455163Sshin{ 10555163Sshin struct in6_drlist dr; 10655163Sshin int s, i; 10755163Sshin u_char ntopbuf[INET6_ADDRSTRLEN]; 10855163Sshin 10955163Sshin if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 110118660Sume warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno)); 11155163Sshin return; 11255163Sshin } 11355163Sshin bzero(&dr, sizeof(dr)); 11455163Sshin strcpy(dr.ifname, "lo0"); /* dummy interface */ 11555163Sshin if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 116118660Sume warnmsg(LOG_ERR, __func__, "ioctl(SIOCGDRLST_IN6): %s", 117118664Sume strerror(errno)); 11855163Sshin goto closeandend; 11955163Sshin } 12055163Sshin 121118664Sume for (i = 0; dr.defrouter[i].if_index && i < PRLSTSIZ; i++) { 12255163Sshin if (ifindex && dr.defrouter[i].if_index == ifindex) { 12355163Sshin /* sanity check */ 12455163Sshin if (!IN6_IS_ADDR_LINKLOCAL(&dr.defrouter[i].rtaddr)) { 125118660Sume warnmsg(LOG_ERR, __func__, 126118664Sume "default router list contains a " 127118664Sume "non-link-local address(%s)", 128118664Sume inet_ntop(AF_INET6, 129118664Sume &dr.defrouter[i].rtaddr, 130118664Sume ntopbuf, INET6_ADDRSTRLEN)); 13155163Sshin continue; /* ignore the address */ 13255163Sshin } 13355163Sshin sendprobe(&dr.defrouter[i].rtaddr, 134118664Sume dr.defrouter[i].if_index); 13555163Sshin } 13655163Sshin } 13755163Sshin 138118664Sumecloseandend: 13955163Sshin close(s); 14055163Sshin} 14155163Sshin 14255163Sshinstatic void 14355163Sshinsendprobe(struct in6_addr *addr, int ifindex) 14455163Sshin{ 14555163Sshin struct sockaddr_in6 sa6_probe; 14655163Sshin struct in6_pktinfo *pi; 14755163Sshin struct cmsghdr *cm; 148118664Sume u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ]; 149118664Sume int hoplimit = 1; 15055163Sshin 15155163Sshin bzero(&sa6_probe, sizeof(sa6_probe)); 15255163Sshin sa6_probe.sin6_family = AF_INET6; 15355163Sshin sa6_probe.sin6_len = sizeof(sa6_probe); 15455163Sshin sa6_probe.sin6_addr = *addr; 15555163Sshin 15655163Sshin sndmhdr.msg_name = (caddr_t)&sa6_probe; 15755163Sshin sndmhdr.msg_iov[0].iov_base = NULL; 15855163Sshin sndmhdr.msg_iov[0].iov_len = 0; 15955163Sshin 16055163Sshin cm = CMSG_FIRSTHDR(&sndmhdr); 16155163Sshin /* specify the outgoing interface */ 16255163Sshin cm->cmsg_level = IPPROTO_IPV6; 16355163Sshin cm->cmsg_type = IPV6_PKTINFO; 16455163Sshin cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 16555163Sshin pi = (struct in6_pktinfo *)CMSG_DATA(cm); 16655163Sshin memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/ 16755163Sshin pi->ipi6_ifindex = ifindex; 16855163Sshin 16955163Sshin /* specify the hop limit of the packet for safety */ 170118664Sume cm = CMSG_NXTHDR(&sndmhdr, cm); 171118664Sume cm->cmsg_level = IPPROTO_IPV6; 172118664Sume cm->cmsg_type = IPV6_HOPLIMIT; 173118664Sume cm->cmsg_len = CMSG_LEN(sizeof(int)); 174118664Sume memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int)); 17555163Sshin 176118660Sume warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s", 177118664Sume inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN), 178118664Sume if_indextoname(ifindex, ifnamebuf)); 17955163Sshin 18055163Sshin if (sendmsg(probesock, &sndmhdr, 0)) 181118660Sume warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s", 182118664Sume if_indextoname(ifindex, ifnamebuf), strerror(errno)); 18355163Sshin} 184