Ignore machine-check MSRs
[freebsd-src/fkvm-freebsd.git] / usr.sbin / rtsold / probe.c
blob61c47bc6a58d0fc688a62c3022541c1dd9c7539d
1 /* $KAME: probe.c,v 1.17 2003/10/05 00:09:36 itojun Exp $ */
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.
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.
31 * $FreeBSD$
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>
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_dl.h>
45 #include <netinet/in.h>
46 #include <netinet6/in6_var.h>
47 #include <netinet/icmp6.h>
48 #include <netinet6/nd6.h>
50 #include <arpa/inet.h>
52 #include <errno.h>
53 #include <unistd.h>
54 #include <string.h>
55 #include <syslog.h>
56 #include <stdlib.h>
58 #include "rtsold.h"
60 static struct msghdr sndmhdr;
61 static struct iovec sndiov[2];
62 static int probesock;
63 static void sendprobe(struct in6_addr *, struct ifinfo *);
65 int
66 probe_init(void)
68 int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
69 CMSG_SPACE(sizeof(int));
70 static u_char *sndcmsgbuf = NULL;
72 if (sndcmsgbuf == NULL &&
73 (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) {
74 warnmsg(LOG_ERR, __func__, "malloc failed");
75 return(-1);
78 if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) {
79 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
80 return(-1);
83 /* make the socket send-only */
84 if (shutdown(probesock, 0)) {
85 warnmsg(LOG_ERR, __func__, "shutdown: %s", strerror(errno));
86 return(-1);
89 /* initialize msghdr for sending packets */
90 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
91 sndmhdr.msg_iov = sndiov;
92 sndmhdr.msg_iovlen = 1;
93 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
94 sndmhdr.msg_controllen = scmsglen;
95 return(0);
99 * Probe if each router in the default router list is still alive.
101 void
102 defrouter_probe(struct ifinfo *ifinfo)
104 u_char ntopbuf[INET6_ADDRSTRLEN];
105 struct in6_drlist dr;
106 int s, i;
107 int ifindex = ifinfo->sdl->sdl_index;
109 if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
110 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
111 return;
113 memset(&dr, 0, sizeof(dr));
114 strlcpy(dr.ifname, "lo0", sizeof dr.ifname); /* dummy interface */
115 if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
116 warnmsg(LOG_ERR, __func__, "ioctl(SIOCGDRLST_IN6): %s",
117 strerror(errno));
118 goto closeandend;
121 for (i = 0; dr.defrouter[i].if_index && i < PRLSTSIZ; i++) {
122 if (ifindex && dr.defrouter[i].if_index == ifindex) {
123 /* sanity check */
124 if (!IN6_IS_ADDR_LINKLOCAL(&dr.defrouter[i].rtaddr)) {
125 warnmsg(LOG_ERR, __func__,
126 "default router list contains a "
127 "non-link-local address(%s)",
128 inet_ntop(AF_INET6,
129 &dr.defrouter[i].rtaddr,
130 ntopbuf, INET6_ADDRSTRLEN));
131 continue; /* ignore the address */
133 sendprobe(&dr.defrouter[i].rtaddr, ifinfo);
137 closeandend:
138 close(s);
141 static void
142 sendprobe(struct in6_addr *addr, struct ifinfo *ifinfo)
144 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
145 struct sockaddr_in6 sa6_probe;
146 struct in6_pktinfo *pi;
147 struct cmsghdr *cm;
148 u_int32_t ifindex = ifinfo->sdl->sdl_index;
149 int hoplimit = 1;
151 memset(&sa6_probe, 0, sizeof(sa6_probe));
152 sa6_probe.sin6_family = AF_INET6;
153 sa6_probe.sin6_len = sizeof(sa6_probe);
154 sa6_probe.sin6_addr = *addr;
155 sa6_probe.sin6_scope_id = ifinfo->linkid;
157 sndmhdr.msg_name = (caddr_t)&sa6_probe;
158 sndmhdr.msg_iov[0].iov_base = NULL;
159 sndmhdr.msg_iov[0].iov_len = 0;
161 cm = CMSG_FIRSTHDR(&sndmhdr);
162 /* specify the outgoing interface */
163 cm->cmsg_level = IPPROTO_IPV6;
164 cm->cmsg_type = IPV6_PKTINFO;
165 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
166 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
167 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
168 pi->ipi6_ifindex = ifindex;
170 /* specify the hop limit of the packet for safety */
171 cm = CMSG_NXTHDR(&sndmhdr, cm);
172 cm->cmsg_level = IPPROTO_IPV6;
173 cm->cmsg_type = IPV6_HOPLIMIT;
174 cm->cmsg_len = CMSG_LEN(sizeof(int));
175 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
177 warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s",
178 inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN),
179 if_indextoname(ifindex, ifnamebuf));
181 if (sendmsg(probesock, &sndmhdr, 0))
182 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
183 if_indextoname(ifindex, ifnamebuf), strerror(errno));