probe.c revision 66776
1/* $KAME$ */ 2 3/* 4 * Copyright (C) 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 18 * 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 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD: head/usr.sbin/rtsold/probe.c 66776 2000-10-06 23:46:52Z kris $ 32 */ 33 34#include <sys/param.h> 35#include <sys/types.h> 36#include <sys/ioctl.h> 37#include <sys/socket.h> 38#include <sys/uio.h> 39#include <sys/queue.h> 40 41#include <net/if.h> 42#if defined(__FreeBSD__) && __FreeBSD__ >= 3 43#include <net/if_var.h> 44#endif /* __FreeBSD__ >= 3 */ 45 46#include <netinet/in.h> 47#include <netinet6/in6_var.h> 48#include <netinet/icmp6.h> 49#include <netinet6/nd6.h> 50 51#include <arpa/inet.h> 52 53#include <errno.h> 54#include <unistd.h> 55#include <string.h> 56#include <syslog.h> 57#include <stdlib.h> 58 59#include "rtsold.h" 60 61static struct msghdr sndmhdr; 62static struct iovec sndiov[2]; 63static int probesock; 64static void sendprobe __P((struct in6_addr *addr, int ifindex)); 65 66 67int 68probe_init() 69{ 70 int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + 71 CMSG_SPACE(sizeof(int)); 72 static u_char *sndcmsgbuf = NULL; 73 74 if (sndcmsgbuf == NULL && 75 (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) { 76 warnmsg(LOG_ERR, __FUNCTION__, "malloc failed"); 77 return(-1); 78 } 79 80 if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) { 81 warnmsg(LOG_ERR, __FUNCTION__, "socket: %s", strerror(errno)); 82 return(-1); 83 } 84 85 /* make the socket send-only */ 86 if (shutdown(probesock, 0)) { 87 warnmsg(LOG_ERR, __FUNCTION__, "shutdown: %s", strerror(errno)); 88 return(-1); 89 } 90 91 /* initialize msghdr for sending packets */ 92 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6); 93 sndmhdr.msg_iov = sndiov; 94 sndmhdr.msg_iovlen = 1; 95 sndmhdr.msg_control = (caddr_t)sndcmsgbuf; 96 sndmhdr.msg_controllen = scmsglen; 97 98 return(0); 99} 100 101/* 102 * Probe if each router in the default router list is still alive. 103 */ 104void 105defrouter_probe(int ifindex) 106{ 107 struct in6_drlist dr; 108 int s, i; 109 u_char ntopbuf[INET6_ADDRSTRLEN]; 110 111 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 112 warnmsg(LOG_ERR, __FUNCTION__, "socket: %s", strerror(errno)); 113 return; 114 } 115 bzero(&dr, sizeof(dr)); 116 strcpy(dr.ifname, "lo0"); /* dummy interface */ 117 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 118 warnmsg(LOG_ERR, __FUNCTION__, "ioctl(SIOCGDRLST_IN6): %s", 119 strerror(errno)); 120 goto closeandend; 121 } 122 123 for(i = 0; dr.defrouter[i].if_index && i < PRLSTSIZ; i++) { 124 if (ifindex && dr.defrouter[i].if_index == ifindex) { 125 /* sanity check */ 126 if (!IN6_IS_ADDR_LINKLOCAL(&dr.defrouter[i].rtaddr)) { 127 warnmsg(LOG_ERR, __FUNCTION__, 128 "default router list contains a " 129 "non-linklocal address(%s)", 130 inet_ntop(AF_INET6, 131 &dr.defrouter[i].rtaddr, 132 ntopbuf, INET6_ADDRSTRLEN)); 133 continue; /* ignore the address */ 134 } 135 sendprobe(&dr.defrouter[i].rtaddr, 136 dr.defrouter[i].if_index); 137 } 138 } 139 140 closeandend: 141 close(s); 142 return; 143} 144 145static void 146sendprobe(struct in6_addr *addr, int ifindex) 147{ 148 struct sockaddr_in6 sa6_probe; 149 struct in6_pktinfo *pi; 150 struct cmsghdr *cm; 151 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];; 152 153 bzero(&sa6_probe, sizeof(sa6_probe)); 154 sa6_probe.sin6_family = AF_INET6; 155 sa6_probe.sin6_len = sizeof(sa6_probe); 156 sa6_probe.sin6_addr = *addr; 157 158 sndmhdr.msg_name = (caddr_t)&sa6_probe; 159 sndmhdr.msg_iov[0].iov_base = NULL; 160 sndmhdr.msg_iov[0].iov_len = 0; 161 162 cm = CMSG_FIRSTHDR(&sndmhdr); 163 /* specify the outgoing interface */ 164 cm->cmsg_level = IPPROTO_IPV6; 165 cm->cmsg_type = IPV6_PKTINFO; 166 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 167 pi = (struct in6_pktinfo *)CMSG_DATA(cm); 168 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/ 169 pi->ipi6_ifindex = ifindex; 170 171 /* specify the hop limit of the packet for safety */ 172 { 173 int hoplimit = 1; 174 175 cm = CMSG_NXTHDR(&sndmhdr, cm); 176 cm->cmsg_level = IPPROTO_IPV6; 177 cm->cmsg_type = IPV6_HOPLIMIT; 178 cm->cmsg_len = CMSG_LEN(sizeof(int)); 179 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int)); 180 } 181 182 warnmsg(LOG_DEBUG, __FUNCTION__, "probe a router %s on %s", 183 inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN), 184 if_indextoname(ifindex, ifnamebuf)); 185 186 if (sendmsg(probesock, &sndmhdr, 0)) 187 warnmsg(LOG_ERR, __FUNCTION__, "sendmsg on %s: %s", 188 if_indextoname(ifindex, ifnamebuf), strerror(errno)); 189 190 return; 191} 192