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