Simplify markup.
[netbsd-mini2440.git] / usr.sbin / rtsold / probe.c
blobb25730cd8eb57243727559ba71f0e7f0117cdaba
1 /* $NetBSD: probe.c,v 1.9 2004/01/03 01:40:32 itojun Exp $ */
2 /* $KAME: probe.c,v 1.15 2002/05/31 21:22:08 itojun Exp $ */
4 /*
5 * Copyright (C) 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <sys/socket.h>
37 #include <sys/uio.h>
38 #include <sys/queue.h>
40 #include <net/if.h>
41 #include <net/if_dl.h>
43 #include <netinet/in.h>
44 #include <netinet6/in6_var.h>
45 #include <netinet/icmp6.h>
46 #include <netinet6/nd6.h>
48 #include <arpa/inet.h>
50 #include <errno.h>
51 #include <unistd.h>
52 #include <string.h>
53 #include <syslog.h>
54 #include <stdlib.h>
56 #include "rtsold.h"
58 static struct msghdr sndmhdr;
59 static struct iovec sndiov[2];
60 static int probesock;
61 static void sendprobe __P((struct in6_addr *, struct ifinfo *));
63 int
64 probe_init(void)
66 int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
67 CMSG_SPACE(sizeof(int));
68 static u_char *sndcmsgbuf = NULL;
70 if (sndcmsgbuf == NULL &&
71 (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) {
72 warnmsg(LOG_ERR, __func__, "malloc failed");
73 return(-1);
76 if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) {
77 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
78 return(-1);
81 /* make the socket send-only */
82 if (shutdown(probesock, 0)) {
83 warnmsg(LOG_ERR, __func__, "shutdown: %s", strerror(errno));
84 return(-1);
87 /* initialize msghdr for sending packets */
88 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
89 sndmhdr.msg_iov = sndiov;
90 sndmhdr.msg_iovlen = 1;
91 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
92 sndmhdr.msg_controllen = scmsglen;
93 return(0);
97 * Probe if each router in the default router list is still alive.
99 void
100 defrouter_probe(struct ifinfo *ifinfo)
102 char ntopbuf[INET6_ADDRSTRLEN];
103 struct in6_drlist dr;
104 int s, i;
105 int ifindex = ifinfo->sdl->sdl_index;
107 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
108 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
109 return;
111 memset(&dr, 0, sizeof(dr));
112 strlcpy(dr.ifname, "lo0", sizeof dr.ifname); /* dummy interface */
113 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
114 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGDRLST_IN6): %s",
115 strerror(errno));
116 goto closeandend;
119 for (i = 0; dr.defrouter[i].if_index && i < PRLSTSIZ; i++) {
120 if (ifindex && dr.defrouter[i].if_index == ifindex) {
121 /* sanity check */
122 if (!IN6_IS_ADDR_LINKLOCAL(&dr.defrouter[i].rtaddr)) {
123 warnmsg(LOG_ERR, __func__,
124 "default router list contains a "
125 "non-link-local address(%s)",
126 inet_ntop(AF_INET6,
127 &dr.defrouter[i].rtaddr,
128 ntopbuf, INET6_ADDRSTRLEN));
129 continue; /* ignore the address */
131 sendprobe(&dr.defrouter[i].rtaddr, ifinfo);
135 closeandend:
136 close(s);
139 static void
140 sendprobe(struct in6_addr *addr, struct ifinfo *ifinfo)
142 char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
143 struct sockaddr_in6 sa6_probe;
144 struct in6_pktinfo *pi;
145 struct cmsghdr *cm;
146 u_int32_t ifindex = ifinfo->sdl->sdl_index;
147 int hoplimit = 1;
149 memset(&sa6_probe, 0, sizeof(sa6_probe));
150 sa6_probe.sin6_family = AF_INET6;
151 sa6_probe.sin6_len = sizeof(sa6_probe);
152 sa6_probe.sin6_addr = *addr;
153 sa6_probe.sin6_scope_id = ifinfo->linkid;
155 sndmhdr.msg_name = (caddr_t)&sa6_probe;
156 sndmhdr.msg_iov[0].iov_base = NULL;
157 sndmhdr.msg_iov[0].iov_len = 0;
159 cm = CMSG_FIRSTHDR(&sndmhdr);
160 /* specify the outgoing interface */
161 cm->cmsg_level = IPPROTO_IPV6;
162 cm->cmsg_type = IPV6_PKTINFO;
163 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
164 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
165 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
166 pi->ipi6_ifindex = ifindex;
168 /* specify the hop limit of the packet for safety */
169 cm = CMSG_NXTHDR(&sndmhdr, cm);
170 cm->cmsg_level = IPPROTO_IPV6;
171 cm->cmsg_type = IPV6_HOPLIMIT;
172 cm->cmsg_len = CMSG_LEN(sizeof(int));
173 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
175 warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s",
176 inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN),
177 if_indextoname(ifindex, ifnamebuf));
179 if (sendmsg(probesock, &sndmhdr, 0))
180 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
181 if_indextoname(ifindex, ifnamebuf), strerror(errno));