probe.c revision 254462
1219732Sume/* $KAME: probe.c,v 1.17 2003/10/05 00:09:36 itojun Exp $ */ 2219732Sume 3219732Sume/* 4219732Sume * Copyright (C) 1998 WIDE Project. 5219732Sume * All rights reserved. 6219732Sume * 7219732Sume * Redistribution and use in source and binary forms, with or without 8219732Sume * modification, are permitted provided that the following conditions 9219732Sume * are met: 10219732Sume * 1. Redistributions of source code must retain the above copyright 11219732Sume * notice, this list of conditions and the following disclaimer. 12219732Sume * 2. Redistributions in binary form must reproduce the above copyright 13219732Sume * notice, this list of conditions and the following disclaimer in the 14219732Sume * documentation and/or other materials provided with the distribution. 15219732Sume * 3. Neither the name of the project nor the names of its contributors 16219732Sume * may be used to endorse or promote products derived from this software 17219732Sume * without specific prior written permission. 18219732Sume * 19219732Sume * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20219732Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21219732Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22219732Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23219732Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24219732Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25219732Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26219732Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27219732Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28219732Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29219732Sume * SUCH DAMAGE. 30219732Sume * 31219732Sume * $FreeBSD: head/usr.sbin/rtsold/probe.c 254462 2013-08-17 19:23:35Z hrs $ 32219732Sume */ 33225524Shrs 34225524Shrs#include <sys/param.h> 35219732Sume#include <sys/types.h> 36219732Sume#include <sys/ioctl.h> 37219732Sume#include <sys/socket.h> 38219732Sume#include <sys/uio.h> 39219732Sume#include <sys/queue.h> 40219732Sume 41219732Sume#include <net/if.h> 42219732Sume#include <net/if_var.h> 43219732Sume#include <net/if_dl.h> 44219732Sume 45219732Sume#include <netinet/in.h> 46219732Sume#include <netinet6/in6_var.h> 47225524Shrs#include <netinet/icmp6.h> 48219732Sume#include <netinet6/nd6.h> 49219732Sume 50219732Sume#include <arpa/inet.h> 51219732Sume 52219732Sume#include <errno.h> 53225524Shrs#include <unistd.h> 54225524Shrs#include <string.h> 55219732Sume#include <syslog.h> 56219732Sume#include <stdlib.h> 57219732Sume 58225524Shrs#include "rtsold.h" 59219732Sume 60219732Sumestatic struct msghdr sndmhdr; 61219732Sumestatic struct iovec sndiov[2]; 62225524Shrsstatic int probesock; 63225524Shrsstatic void sendprobe(struct in6_addr *, struct ifinfo *); 64225524Shrs 65219732Sumeint 66219732Sumeprobe_init(void) 67225524Shrs{ 68219732Sume int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + 69219732Sume CMSG_SPACE(sizeof(int)); 70219732Sume static u_char *sndcmsgbuf = NULL; 71225524Shrs 72219732Sume if (sndcmsgbuf == NULL && 73219732Sume (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) { 74219732Sume warnmsg(LOG_ERR, __func__, "malloc failed"); 75219732Sume return (-1); 76219732Sume } 77219732Sume 78219732Sume if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) { 79225524Shrs warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno)); 80219732Sume return (-1); 81225524Shrs } 82219732Sume 83219732Sume /* make the socket send-only */ 84219732Sume if (shutdown(probesock, 0)) { 85219732Sume warnmsg(LOG_ERR, __func__, "shutdown: %s", strerror(errno)); 86219732Sume return (-1); 87225524Shrs } 88219732Sume 89225524Shrs /* initialize msghdr for sending packets */ 90219732Sume sndmhdr.msg_namelen = sizeof(struct sockaddr_in6); 91219732Sume sndmhdr.msg_iov = sndiov; 92219732Sume sndmhdr.msg_iovlen = 1; 93219732Sume sndmhdr.msg_control = (caddr_t)sndcmsgbuf; 94219732Sume sndmhdr.msg_controllen = scmsglen; 95219732Sume 96219732Sume return (0); 97} 98 99/* 100 * Probe if each router in the default router list is still alive. 101 */ 102void 103defrouter_probe(struct ifinfo *ifinfo) 104{ 105 u_char ntopbuf[INET6_ADDRSTRLEN]; 106 struct in6_drlist dr; 107 int s, i; 108 int ifindex = ifinfo->sdl->sdl_index; 109 110 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 111 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno)); 112 return; 113 } 114 memset(&dr, 0, sizeof(dr)); 115 strlcpy(dr.ifname, "lo0", sizeof dr.ifname); /* dummy interface */ 116 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) { 117 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGDRLST_IN6): %s", 118 strerror(errno)); 119 goto closeandend; 120 } 121 122 for (i = 0; i < DRLSTSIZ && dr.defrouter[i].if_index; i++) { 123 if (ifindex && dr.defrouter[i].if_index == ifindex) { 124 /* sanity check */ 125 if (!IN6_IS_ADDR_LINKLOCAL(&dr.defrouter[i].rtaddr)) { 126 warnmsg(LOG_ERR, __func__, 127 "default router list contains a " 128 "non-link-local address(%s)", 129 inet_ntop(AF_INET6, 130 &dr.defrouter[i].rtaddr, 131 ntopbuf, INET6_ADDRSTRLEN)); 132 continue; /* ignore the address */ 133 } 134 sendprobe(&dr.defrouter[i].rtaddr, ifinfo); 135 } 136 } 137 138closeandend: 139 close(s); 140} 141 142static void 143sendprobe(struct in6_addr *addr, struct ifinfo *ifinfo) 144{ 145 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ]; 146 struct sockaddr_in6 sa6_probe; 147 struct in6_pktinfo *pi; 148 struct cmsghdr *cm; 149 u_int32_t ifindex = ifinfo->sdl->sdl_index; 150 int hoplimit = 1; 151 152 memset(&sa6_probe, 0, sizeof(sa6_probe)); 153 sa6_probe.sin6_family = AF_INET6; 154 sa6_probe.sin6_len = sizeof(sa6_probe); 155 sa6_probe.sin6_addr = *addr; 156 sa6_probe.sin6_scope_id = ifinfo->linkid; 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 *)(void *)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 cm = CMSG_NXTHDR(&sndmhdr, cm); 173 cm->cmsg_level = IPPROTO_IPV6; 174 cm->cmsg_type = IPV6_HOPLIMIT; 175 cm->cmsg_len = CMSG_LEN(sizeof(int)); 176 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int)); 177 178 warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s", 179 inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN), 180 if_indextoname(ifindex, ifnamebuf)); 181 182 if (sendmsg(probesock, &sndmhdr, 0)) 183 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s", 184 if_indextoname(ifindex, ifnamebuf), strerror(errno)); 185} 186