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.2 2003/06/17 04:30:03 dillon 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 = {sizeof(sin6_allrouters), AF_INET6};
78 struct icmp6_filter filt;
79 static u_char answer[1500];
80 int rcvcmsglen, sndcmsglen;
81 static u_char *rcvcmsgbuf = NULL, *sndcmsgbuf = NULL;
83 sndcmsglen = rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
84 CMSG_SPACE(sizeof(int));
85 if (rcvcmsgbuf == NULL && (rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
86 warnmsg(LOG_ERR, __FUNCTION__,
87 "malloc for receive msghdr failed");
90 if (sndcmsgbuf == NULL && (sndcmsgbuf = malloc(sndcmsglen)) == NULL) {
91 warnmsg(LOG_ERR, __FUNCTION__,
92 "malloc for send msghdr failed");
95 memset(&sin6_allrouters, 0, sizeof(struct sockaddr_in6));
96 sin6_allrouters.sin6_family = AF_INET6;
97 sin6_allrouters.sin6_len = sizeof(sin6_allrouters);
98 if (inet_pton(AF_INET6, ALLROUTER,
99 &sin6_allrouters.sin6_addr.s6_addr) != 1) {
100 warnmsg(LOG_ERR, __FUNCTION__, "inet_pton failed for %s",
105 if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
106 warnmsg(LOG_ERR, __FUNCTION__, "socket: %s", strerror(errno));
110 /* specify to tell receiving interface */
112 #ifdef IPV6_RECVPKTINFO
113 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
115 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_RECVPKTINFO: %s",
119 #else /* old adv. API */
120 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
122 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_PKTINFO: %s",
129 /* specify to tell value of hoplimit field of received IP6 hdr */
130 #ifdef IPV6_RECVHOPLIMIT
131 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
133 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_RECVHOPLIMIT: %s",
137 #else /* old adv. API */
138 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
140 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_HOPLIMIT: %s",
146 /* specfiy to accept only router advertisements on the socket */
147 ICMP6_FILTER_SETBLOCKALL(&filt);
148 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
149 if (setsockopt(rssock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
150 sizeof(filt)) == -1) {
151 warnmsg(LOG_ERR, __FUNCTION__, "setsockopt(ICMP6_FILTER): %s",
156 /* initialize msghdr for receiving packets */
157 rcviov[0].iov_base = (caddr_t)answer;
158 rcviov[0].iov_len = sizeof(answer);
159 rcvmhdr.msg_name = (caddr_t)&from;
160 rcvmhdr.msg_namelen = sizeof(from);
161 rcvmhdr.msg_iov = rcviov;
162 rcvmhdr.msg_iovlen = 1;
163 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
164 rcvmhdr.msg_controllen = rcvcmsglen;
166 /* initialize msghdr for sending packets */
167 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
168 sndmhdr.msg_iov = sndiov;
169 sndmhdr.msg_iovlen = 1;
170 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
171 sndmhdr.msg_controllen = sndcmsglen;
177 sendpacket(struct ifinfo *ifinfo)
181 struct in6_pktinfo *pi;
183 sndmhdr.msg_name = (caddr_t)&sin6_allrouters;
184 sndmhdr.msg_iov[0].iov_base = (caddr_t)ifinfo->rs_data;
185 sndmhdr.msg_iov[0].iov_len = ifinfo->rs_datalen;
187 cm = CMSG_FIRSTHDR(&sndmhdr);
188 /* specify the outgoing interface */
189 cm->cmsg_level = IPPROTO_IPV6;
190 cm->cmsg_type = IPV6_PKTINFO;
191 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
192 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
193 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
194 pi->ipi6_ifindex = ifinfo->sdl->sdl_index;
196 /* specify the hop limit of the packet */
200 cm = CMSG_NXTHDR(&sndmhdr, cm);
201 cm->cmsg_level = IPPROTO_IPV6;
202 cm->cmsg_type = IPV6_HOPLIMIT;
203 cm->cmsg_len = CMSG_LEN(sizeof(int));
204 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
208 __FUNCTION__, "send RS on %s, whose state is %d",
209 ifinfo->ifname, ifinfo->state);
211 i = sendmsg(rssock, &sndmhdr, 0);
213 if (i < 0 || i != ifinfo->rs_datalen) {
215 * ENETDOWN is not so serious, especially when using several
216 * network cards on a mobile node. We ignore it.
218 if (errno != ENETDOWN || dflag > 0)
219 warnmsg(LOG_ERR, __FUNCTION__, "sendmsg on %s: %s",
220 ifinfo->ifname, strerror(errno));
232 struct icmp6_hdr *icp;
235 struct in6_pktinfo *pi = NULL;
236 struct ifinfo *ifi = NULL;
237 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
240 if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) {
241 warnmsg(LOG_ERR, __FUNCTION__, "recvmsg: %s", strerror(errno));
245 /* extract optional information via Advanced API */
246 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
248 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
249 if (cm->cmsg_level == IPPROTO_IPV6 &&
250 cm->cmsg_type == IPV6_PKTINFO &&
251 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
252 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
253 ifindex = pi->ipi6_ifindex;
255 if (cm->cmsg_level == IPPROTO_IPV6 &&
256 cm->cmsg_type == IPV6_HOPLIMIT &&
257 cm->cmsg_len == CMSG_LEN(sizeof(int)))
258 hlimp = (int *)CMSG_DATA(cm);
263 __FUNCTION__, "failed to get receiving interface");
268 __FUNCTION__, "failed to get receiving hop limit");
272 if (i < sizeof(struct nd_router_advert)) {
274 __FUNCTION__, "packet size(%d) is too short", i);
278 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
280 if (icp->icmp6_type != ND_ROUTER_ADVERT) {
281 warnmsg(LOG_ERR, __FUNCTION__,
282 "invalid icmp type(%d) from %s on %s", icp->icmp6_type,
283 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
285 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
289 if (icp->icmp6_code != 0) {
290 warnmsg(LOG_ERR, __FUNCTION__,
291 "invalid icmp code(%d) from %s on %s", icp->icmp6_code,
292 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
294 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
299 warnmsg(LOG_NOTICE, __FUNCTION__,
300 "invalid RA with hop limit(%d) from %s on %s",
302 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
304 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
308 if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
309 warnmsg(LOG_NOTICE, __FUNCTION__,
310 "invalid RA with non link-local source from %s on %s",
311 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
313 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
317 /* xxx: more validation? */
319 if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) {
320 warnmsg(LOG_NOTICE, __FUNCTION__,
321 "received RA from %s on an unexpeced IF(%s)",
322 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
324 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
328 warnmsg(LOG_DEBUG, __FUNCTION__,
329 "received RA from %s on %s, state is %d",
330 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
332 ifi->ifname, ifi->state);
337 case IFS_IDLE: /* should be ignored */
338 case IFS_DELAY: /* right? */
341 ifi->state = IFS_IDLE;
343 rtsol_timer_update(ifi);