]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/rtsold/probe.c
Fix ICMPv6 use-after-free in error message handling.
[FreeBSD/FreeBSD.git] / usr.sbin / rtsold / probe.c
1 /*      $KAME: probe.c,v 1.17 2003/10/05 00:09:36 itojun Exp $  */
2
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1998 WIDE Project.
7  * All rights reserved.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the project nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * $FreeBSD$
34  */
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
41 #include <sys/uio.h>
42 #include <sys/queue.h>
43
44 #include <net/if.h>
45 #include <net/if_dl.h>
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 *, struct ifinfo *);
66
67 int
68 probe_init(void)
69 {
70         int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
71             CMSG_SPACE(sizeof(int));
72         static u_char *sndcmsgbuf = NULL;
73
74         if (sndcmsgbuf == NULL &&
75             (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) {
76                 warnmsg(LOG_ERR, __func__, "malloc failed");
77                 return (-1);
78         }
79
80         if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) {
81                 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
82                 return (-1);
83         }
84
85         /* initialize msghdr for sending packets */
86         sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
87         sndmhdr.msg_iov = sndiov;
88         sndmhdr.msg_iovlen = 1;
89         sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
90         sndmhdr.msg_controllen = scmsglen;
91
92         return (0);
93 }
94
95 /*
96  * Probe if each router in the default router list is still alive.
97  */
98 void
99 defrouter_probe(struct ifinfo *ifinfo)
100 {
101         struct in6_defrouter *p, *ep;
102         int ifindex, mib[4];
103         char *buf, ntopbuf[INET6_ADDRSTRLEN];
104         size_t l;
105
106         ifindex = ifinfo->sdl->sdl_index;
107         if (ifindex == 0)
108                 return;
109         mib[0] = CTL_NET;
110         mib[1] = PF_INET6;
111         mib[2] = IPPROTO_ICMPV6;
112         mib[3] = ICMPV6CTL_ND6_DRLIST;
113         if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0) {
114                 warnmsg(LOG_ERR, __func__, "sysctl(ICMPV6CTL_ND6_DRLIST): %s",
115                     strerror(errno));
116                 return;
117         }
118         if (l == 0)
119                 return;
120         buf = malloc(l);
121         if (buf == NULL) {
122                 warnmsg(LOG_ERR, __func__, "malloc(): %s", strerror(errno));
123                 return;
124         }
125         if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) {
126                 warnmsg(LOG_ERR, __func__, "sysctl(ICMPV6CTL_ND6_DRLIST): %s",
127                     strerror(errno));
128                 free(buf);
129                 return;
130         }
131         ep = (struct in6_defrouter *)(void *)(buf + l);
132         for (p = (struct in6_defrouter *)(void *)buf; p < ep; p++) {
133                 if (ifindex != p->if_index)
134                         continue;
135                 if (!IN6_IS_ADDR_LINKLOCAL(&p->rtaddr.sin6_addr)) {
136                         warnmsg(LOG_ERR, __func__,
137                             "default router list contains a "
138                             "non-link-local address(%s)",
139                             inet_ntop(AF_INET6, &p->rtaddr.sin6_addr, ntopbuf,
140                             INET6_ADDRSTRLEN));
141                         continue; /* ignore the address */
142                 }
143                 sendprobe(&p->rtaddr.sin6_addr, ifinfo);
144         }
145         free(buf);
146 }
147
148 static void
149 sendprobe(struct in6_addr *addr, struct ifinfo *ifinfo)
150 {
151         u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
152         struct sockaddr_in6 sa6_probe;
153         struct in6_pktinfo *pi;
154         struct cmsghdr *cm;
155         u_int32_t ifindex = ifinfo->sdl->sdl_index;
156         int hoplimit = 1;
157
158         memset(&sa6_probe, 0, sizeof(sa6_probe));
159         sa6_probe.sin6_family = AF_INET6;
160         sa6_probe.sin6_len = sizeof(sa6_probe);
161         sa6_probe.sin6_addr = *addr;
162         sa6_probe.sin6_scope_id = ifinfo->linkid;
163
164         sndmhdr.msg_name = (caddr_t)&sa6_probe;
165         sndmhdr.msg_iov[0].iov_base = NULL;
166         sndmhdr.msg_iov[0].iov_len = 0;
167
168         cm = CMSG_FIRSTHDR(&sndmhdr);
169         /* specify the outgoing interface */
170         cm->cmsg_level = IPPROTO_IPV6;
171         cm->cmsg_type = IPV6_PKTINFO;
172         cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
173         pi = (struct in6_pktinfo *)(void *)CMSG_DATA(cm);
174         memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));       /*XXX*/
175         pi->ipi6_ifindex = ifindex;
176
177         /* specify the hop limit of the packet for safety */
178         cm = CMSG_NXTHDR(&sndmhdr, cm);
179         cm->cmsg_level = IPPROTO_IPV6;
180         cm->cmsg_type = IPV6_HOPLIMIT;
181         cm->cmsg_len = CMSG_LEN(sizeof(int));
182         memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
183
184         warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s",
185             inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN),
186             if_indextoname(ifindex, ifnamebuf));
187
188         if (sendmsg(probesock, &sndmhdr, 0))
189                 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
190                     if_indextoname(ifindex, ifnamebuf), strerror(errno));
191 }