]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/rtsold/probe.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / rtsold / probe.c
1 /*      $KAME: probe.c,v 1.17 2003/10/05 00:09:36 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$
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/sysctl.h>
39 #include <sys/uio.h>
40 #include <sys/queue.h>
41
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <net/if_dl.h>
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
61 static struct msghdr sndmhdr;
62 static struct iovec sndiov[2];
63 static int probesock;
64 static void sendprobe(struct in6_addr *, struct ifinfo *);
65
66 int
67 probe_init(void)
68 {
69         int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
70             CMSG_SPACE(sizeof(int));
71         static u_char *sndcmsgbuf = NULL;
72
73         if (sndcmsgbuf == NULL &&
74             (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) {
75                 warnmsg(LOG_ERR, __func__, "malloc failed");
76                 return (-1);
77         }
78
79         if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) {
80                 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
81                 return (-1);
82         }
83
84         /* make the socket send-only */
85         if (shutdown(probesock, 0)) {
86                 warnmsg(LOG_ERR, __func__, "shutdown: %s", strerror(errno));
87                 return (-1);
88         }
89
90         /* initialize msghdr for sending packets */
91         sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
92         sndmhdr.msg_iov = sndiov;
93         sndmhdr.msg_iovlen = 1;
94         sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
95         sndmhdr.msg_controllen = scmsglen;
96
97         return (0);
98 }
99
100 /*
101  * Probe if each router in the default router list is still alive.
102  */
103 void
104 defrouter_probe(struct ifinfo *ifinfo)
105 {
106         struct in6_defrouter *p, *ep;
107         int ifindex, mib[4];
108         char *buf, ntopbuf[INET6_ADDRSTRLEN];
109         size_t l;
110
111         ifindex = ifinfo->sdl->sdl_index;
112         if (ifindex == 0)
113                 return;
114         mib[0] = CTL_NET;
115         mib[1] = PF_INET6;
116         mib[2] = IPPROTO_ICMPV6;
117         mib[3] = ICMPV6CTL_ND6_DRLIST;
118         if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0) {
119                 warnmsg(LOG_ERR, __func__, "sysctl(ICMPV6CTL_ND6_DRLIST): %s",
120                     strerror(errno));
121                 return;
122         }
123         if (l == 0)
124                 return;
125         buf = malloc(l);
126         if (buf == NULL) {
127                 warnmsg(LOG_ERR, __func__, "malloc(): %s", strerror(errno));
128                 return;
129         }
130         if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) {
131                 warnmsg(LOG_ERR, __func__, "sysctl(ICMPV6CTL_ND6_DRLIST): %s",
132                     strerror(errno));
133                 free(buf);
134                 return;
135         }
136         ep = (struct in6_defrouter *)(void *)(buf + l);
137         for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) {
138                 if (ifindex != p->if_index)
139                         continue;
140                 if (!IN6_IS_ADDR_LINKLOCAL(&p->rtaddr.sin6_addr)) {
141                         warnmsg(LOG_ERR, __func__,
142                             "default router list contains a "
143                             "non-link-local address(%s)",
144                             inet_ntop(AF_INET6, &p->rtaddr.sin6_addr, ntopbuf,
145                             INET6_ADDRSTRLEN));
146                         continue; /* ignore the address */
147                 }
148                 sendprobe(&p->rtaddr.sin6_addr, ifinfo);
149         }
150         free(buf);
151 }
152
153 static void
154 sendprobe(struct in6_addr *addr, struct ifinfo *ifinfo)
155 {
156         u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
157         struct sockaddr_in6 sa6_probe;
158         struct in6_pktinfo *pi;
159         struct cmsghdr *cm;
160         u_int32_t ifindex = ifinfo->sdl->sdl_index;
161         int hoplimit = 1;
162
163         memset(&sa6_probe, 0, sizeof(sa6_probe));
164         sa6_probe.sin6_family = AF_INET6;
165         sa6_probe.sin6_len = sizeof(sa6_probe);
166         sa6_probe.sin6_addr = *addr;
167         sa6_probe.sin6_scope_id = ifinfo->linkid;
168
169         sndmhdr.msg_name = (caddr_t)&sa6_probe;
170         sndmhdr.msg_iov[0].iov_base = NULL;
171         sndmhdr.msg_iov[0].iov_len = 0;
172
173         cm = CMSG_FIRSTHDR(&sndmhdr);
174         /* specify the outgoing interface */
175         cm->cmsg_level = IPPROTO_IPV6;
176         cm->cmsg_type = IPV6_PKTINFO;
177         cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
178         pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
179         memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));       /*XXX*/
180         pi->ipi6_ifindex = ifindex;
181
182         /* specify the hop limit of the packet for safety */
183         cm = CMSG_NXTHDR(&sndmhdr, cm);
184         cm->cmsg_level = IPPROTO_IPV6;
185         cm->cmsg_type = IPV6_HOPLIMIT;
186         cm->cmsg_len = CMSG_LEN(sizeof(int));
187         memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
188
189         warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s",
190             inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN),
191             if_indextoname(ifindex, ifnamebuf));
192
193         if (sendmsg(probesock, &sndmhdr, 0))
194                 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
195                     if_indextoname(ifindex, ifnamebuf), strerror(errno));
196 }