probe.c revision 118787
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: head/usr.sbin/rtsold/probe.c 118787 2003-08-11 15:51:54Z ume $
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/uio.h>
39#include <sys/queue.h>
40
41#include <net/if.h>
42#include <net/if_var.h>
43
44#include <netinet/in.h>
45#include <netinet6/in6_var.h>
46#include <netinet/icmp6.h>
47#include <netinet6/nd6.h>
48
49#include <arpa/inet.h>
50
51#include <errno.h>
52#include <unistd.h>
53#include <string.h>
54#include <syslog.h>
55#include <stdlib.h>
56
57#include "rtsold.h"
58
59static struct msghdr sndmhdr;
60static struct iovec sndiov[2];
61static int probesock;
62static void sendprobe __P((struct in6_addr *, int));
63
64int
65probe_init()
66{
67	int scmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
68	    CMSG_SPACE(sizeof(int));
69	static u_char *sndcmsgbuf = NULL;
70
71	if (sndcmsgbuf == NULL &&
72	    (sndcmsgbuf = (u_char *)malloc(scmsglen)) == NULL) {
73		warnmsg(LOG_ERR, __func__, "malloc failed");
74		return(-1);
75	}
76
77	if ((probesock = socket(AF_INET6, SOCK_RAW, IPPROTO_NONE)) < 0) {
78		warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
79		return(-1);
80	}
81
82	/* make the socket send-only */
83	if (shutdown(probesock, 0)) {
84		warnmsg(LOG_ERR, __func__, "shutdown: %s", strerror(errno));
85		return(-1);
86	}
87
88	/* initialize msghdr for sending packets */
89	sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
90	sndmhdr.msg_iov = sndiov;
91	sndmhdr.msg_iovlen = 1;
92	sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
93	sndmhdr.msg_controllen = scmsglen;
94	return(0);
95}
96
97/*
98 * Probe if each router in the default router list is still alive.
99 */
100void
101defrouter_probe(int ifindex)
102{
103	struct in6_drlist dr;
104	int s, i;
105	u_char ntopbuf[INET6_ADDRSTRLEN];
106
107	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
108		warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
109		return;
110	}
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;
117	}
118
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 */
130			}
131			sendprobe(&dr.defrouter[i].rtaddr,
132			    dr.defrouter[i].if_index);
133		}
134	}
135
136closeandend:
137	close(s);
138}
139
140static void
141sendprobe(struct in6_addr *addr, int ifindex)
142{
143	struct sockaddr_in6 sa6_probe;
144	struct in6_pktinfo *pi;
145	struct cmsghdr *cm;
146	u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
147	int hoplimit = 1;
148
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
154	sndmhdr.msg_name = (caddr_t)&sa6_probe;
155	sndmhdr.msg_iov[0].iov_base = NULL;
156	sndmhdr.msg_iov[0].iov_len = 0;
157
158	cm = CMSG_FIRSTHDR(&sndmhdr);
159	/* specify the outgoing interface */
160	cm->cmsg_level = IPPROTO_IPV6;
161	cm->cmsg_type = IPV6_PKTINFO;
162	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
163	pi = (struct in6_pktinfo *)CMSG_DATA(cm);
164	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));	/*XXX*/
165	pi->ipi6_ifindex = ifindex;
166
167	/* specify the hop limit of the packet for safety */
168	cm = CMSG_NXTHDR(&sndmhdr, cm);
169	cm->cmsg_level = IPPROTO_IPV6;
170	cm->cmsg_type = IPV6_HOPLIMIT;
171	cm->cmsg_len = CMSG_LEN(sizeof(int));
172	memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
173
174	warnmsg(LOG_DEBUG, __func__, "probe a router %s on %s",
175	    inet_ntop(AF_INET6, addr, ntopbuf, INET6_ADDRSTRLEN),
176	    if_indextoname(ifindex, ifnamebuf));
177
178	if (sendmsg(probesock, &sndmhdr, 0))
179		warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
180		    if_indextoname(ifindex, ifnamebuf), strerror(errno));
181}
182