1 /* $KAME: rtsol.c,v 1.12 2001/11/12 11:47:11 jinmei Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
31 * $FreeBSD: src/usr.sbin/rtsold/rtsol.c,v 1.1.2.4 2002/04/24 10:22:30 suz Exp $
32 * $DragonFly: src/usr.sbin/rtsold/rtsol.c,v 1.6 2006/10/17 19:20:49 pavalos Exp $
35 #include <sys/param.h>
36 #include <sys/socket.h>
39 #include <sys/queue.h>
42 #include <net/route.h>
43 #include <net/if_dl.h>
45 #include <netinet/in.h>
46 #include <netinet/ip6.h>
47 #include <netinet6/ip6_var.h>
48 #include <netinet/icmp6.h>
50 #include <arpa/inet.h>
62 #define ALLROUTER "ff02::2"
64 static struct msghdr rcvmhdr;
65 static struct msghdr sndmhdr;
66 static struct iovec rcviov[2];
67 static struct iovec sndiov[2];
68 static struct sockaddr_in6 from;
72 static struct sockaddr_in6 sin6_allrouters = {
73 sizeof(sin6_allrouters),
85 struct icmp6_filter filt;
86 static u_char answer[1500];
87 int rcvcmsglen, sndcmsglen;
88 static u_char *rcvcmsgbuf = NULL, *sndcmsgbuf = NULL;
90 sndcmsglen = rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
91 CMSG_SPACE(sizeof(int));
92 if (rcvcmsgbuf == NULL && (rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
93 warnmsg(LOG_ERR, __func__,
94 "malloc for receive msghdr failed");
97 if (sndcmsgbuf == NULL && (sndcmsgbuf = malloc(sndcmsglen)) == NULL) {
98 warnmsg(LOG_ERR, __func__,
99 "malloc for send msghdr failed");
102 memset(&sin6_allrouters, 0, sizeof(struct sockaddr_in6));
103 sin6_allrouters.sin6_family = AF_INET6;
104 sin6_allrouters.sin6_len = sizeof(sin6_allrouters);
105 if (inet_pton(AF_INET6, ALLROUTER,
106 &sin6_allrouters.sin6_addr.s6_addr) != 1) {
107 warnmsg(LOG_ERR, __func__, "inet_pton failed for %s",
112 if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
113 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno));
117 /* specify to tell receiving interface */
119 #ifdef IPV6_RECVPKTINFO
120 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
122 warnmsg(LOG_ERR, __func__, "IPV6_RECVPKTINFO: %s",
126 #else /* old adv. API */
127 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
129 warnmsg(LOG_ERR, __func__, "IPV6_PKTINFO: %s",
136 /* specify to tell value of hoplimit field of received IP6 hdr */
137 #ifdef IPV6_RECVHOPLIMIT
138 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
140 warnmsg(LOG_ERR, __func__, "IPV6_RECVHOPLIMIT: %s",
144 #else /* old adv. API */
145 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
147 warnmsg(LOG_ERR, __func__, "IPV6_HOPLIMIT: %s",
153 /* specfiy to accept only router advertisements on the socket */
154 ICMP6_FILTER_SETBLOCKALL(&filt);
155 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
156 if (setsockopt(rssock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
157 sizeof(filt)) == -1) {
158 warnmsg(LOG_ERR, __func__, "setsockopt(ICMP6_FILTER): %s",
163 /* initialize msghdr for receiving packets */
164 rcviov[0].iov_base = (caddr_t)answer;
165 rcviov[0].iov_len = sizeof(answer);
166 rcvmhdr.msg_name = (caddr_t)&from;
167 rcvmhdr.msg_namelen = sizeof(from);
168 rcvmhdr.msg_iov = rcviov;
169 rcvmhdr.msg_iovlen = 1;
170 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
171 rcvmhdr.msg_controllen = rcvcmsglen;
173 /* initialize msghdr for sending packets */
174 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
175 sndmhdr.msg_iov = sndiov;
176 sndmhdr.msg_iovlen = 1;
177 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
178 sndmhdr.msg_controllen = sndcmsglen;
184 sendpacket(struct ifinfo *ifinfo)
188 struct in6_pktinfo *pi;
190 sndmhdr.msg_name = (caddr_t)&sin6_allrouters;
191 sndmhdr.msg_iov[0].iov_base = (caddr_t)ifinfo->rs_data;
192 sndmhdr.msg_iov[0].iov_len = ifinfo->rs_datalen;
194 cm = CMSG_FIRSTHDR(&sndmhdr);
195 /* specify the outgoing interface */
196 cm->cmsg_level = IPPROTO_IPV6;
197 cm->cmsg_type = IPV6_PKTINFO;
198 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
199 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
200 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
201 pi->ipi6_ifindex = ifinfo->sdl->sdl_index;
203 /* specify the hop limit of the packet */
207 cm = CMSG_NXTHDR(&sndmhdr, cm);
208 cm->cmsg_level = IPPROTO_IPV6;
209 cm->cmsg_type = IPV6_HOPLIMIT;
210 cm->cmsg_len = CMSG_LEN(sizeof(int));
211 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
215 __func__, "send RS on %s, whose state is %d",
216 ifinfo->ifname, ifinfo->state);
218 i = sendmsg(rssock, &sndmhdr, 0);
220 if (i < 0 || (size_t)i != ifinfo->rs_datalen) {
222 * ENETDOWN is not so serious, especially when using several
223 * network cards on a mobile node. We ignore it.
225 if (errno != ENETDOWN || dflag > 0)
226 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s",
227 ifinfo->ifname, strerror(errno));
239 struct icmp6_hdr *icp;
242 struct in6_pktinfo *pi = NULL;
243 struct ifinfo *ifi = NULL;
244 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
247 if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) {
248 warnmsg(LOG_ERR, __func__, "recvmsg: %s", strerror(errno));
252 /* extract optional information via Advanced API */
253 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
255 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
256 if (cm->cmsg_level == IPPROTO_IPV6 &&
257 cm->cmsg_type == IPV6_PKTINFO &&
258 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
259 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
260 ifindex = pi->ipi6_ifindex;
262 if (cm->cmsg_level == IPPROTO_IPV6 &&
263 cm->cmsg_type == IPV6_HOPLIMIT &&
264 cm->cmsg_len == CMSG_LEN(sizeof(int)))
265 hlimp = (int *)CMSG_DATA(cm);
270 __func__, "failed to get receiving interface");
275 __func__, "failed to get receiving hop limit");
279 if ((size_t)i < sizeof(struct nd_router_advert)) {
281 __func__, "packet size(%d) is too short", i);
285 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
287 if (icp->icmp6_type != ND_ROUTER_ADVERT) {
288 warnmsg(LOG_ERR, __func__,
289 "invalid icmp type(%d) from %s on %s", icp->icmp6_type,
290 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
292 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
296 if (icp->icmp6_code != 0) {
297 warnmsg(LOG_ERR, __func__,
298 "invalid icmp code(%d) from %s on %s", icp->icmp6_code,
299 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
301 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
306 warnmsg(LOG_NOTICE, __func__,
307 "invalid RA with hop limit(%d) from %s on %s",
309 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
311 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
315 if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
316 warnmsg(LOG_NOTICE, __func__,
317 "invalid RA with non link-local source from %s on %s",
318 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
320 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
324 /* xxx: more validation? */
326 if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) {
327 warnmsg(LOG_NOTICE, __func__,
328 "received RA from %s on an unexpected IF(%s)",
329 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
331 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
335 warnmsg(LOG_DEBUG, __func__,
336 "received RA from %s on %s, state is %d",
337 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
339 ifi->ifname, ifi->state);
344 case IFS_IDLE: /* should be ignored */
345 case IFS_DELAY: /* right? */
348 ifi->state = IFS_IDLE;
350 rtsol_timer_update(ifi);