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
34 #include <sys/param.h>
35 #include <sys/socket.h>
38 #include <sys/queue.h>
41 #include <net/route.h>
42 #include <net/if_dl.h>
44 #include <netinet/in.h>
45 #include <netinet/ip6.h>
46 #include <netinet6/ip6_var.h>
47 #include <netinet/icmp6.h>
49 #include <arpa/inet.h>
61 #define ALLROUTER "ff02::2"
63 static struct msghdr rcvmhdr;
64 static struct msghdr sndmhdr;
65 static struct iovec rcviov[2];
66 static struct iovec sndiov[2];
67 static struct sockaddr_in6 from;
71 static struct sockaddr_in6 sin6_allrouters = {sizeof(sin6_allrouters), AF_INET6};
77 struct icmp6_filter filt;
78 static u_char answer[1500];
79 int rcvcmsglen, sndcmsglen;
80 static u_char *rcvcmsgbuf = NULL, *sndcmsgbuf = NULL;
82 sndcmsglen = rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
83 CMSG_SPACE(sizeof(int));
84 if (rcvcmsgbuf == NULL && (rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) {
85 warnmsg(LOG_ERR, __FUNCTION__,
86 "malloc for receive msghdr failed");
89 if (sndcmsgbuf == NULL && (sndcmsgbuf = malloc(sndcmsglen)) == NULL) {
90 warnmsg(LOG_ERR, __FUNCTION__,
91 "malloc for send msghdr failed");
94 memset(&sin6_allrouters, 0, sizeof(struct sockaddr_in6));
95 sin6_allrouters.sin6_family = AF_INET6;
96 sin6_allrouters.sin6_len = sizeof(sin6_allrouters);
97 if (inet_pton(AF_INET6, ALLROUTER,
98 &sin6_allrouters.sin6_addr.s6_addr) != 1) {
99 warnmsg(LOG_ERR, __FUNCTION__, "inet_pton failed for %s",
104 if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
105 warnmsg(LOG_ERR, __FUNCTION__, "socket: %s", strerror(errno));
109 /* specify to tell receiving interface */
111 #ifdef IPV6_RECVPKTINFO
112 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
114 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_RECVPKTINFO: %s",
118 #else /* old adv. API */
119 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
121 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_PKTINFO: %s",
128 /* specify to tell value of hoplimit field of received IP6 hdr */
129 #ifdef IPV6_RECVHOPLIMIT
130 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
132 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_RECVHOPLIMIT: %s",
136 #else /* old adv. API */
137 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
139 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_HOPLIMIT: %s",
145 /* specfiy to accept only router advertisements on the socket */
146 ICMP6_FILTER_SETBLOCKALL(&filt);
147 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
148 if (setsockopt(rssock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
149 sizeof(filt)) == -1) {
150 warnmsg(LOG_ERR, __FUNCTION__, "setsockopt(ICMP6_FILTER): %s",
155 /* initialize msghdr for receiving packets */
156 rcviov[0].iov_base = (caddr_t)answer;
157 rcviov[0].iov_len = sizeof(answer);
158 rcvmhdr.msg_name = (caddr_t)&from;
159 rcvmhdr.msg_namelen = sizeof(from);
160 rcvmhdr.msg_iov = rcviov;
161 rcvmhdr.msg_iovlen = 1;
162 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
163 rcvmhdr.msg_controllen = rcvcmsglen;
165 /* initialize msghdr for sending packets */
166 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
167 sndmhdr.msg_iov = sndiov;
168 sndmhdr.msg_iovlen = 1;
169 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
170 sndmhdr.msg_controllen = sndcmsglen;
176 sendpacket(struct ifinfo *ifinfo)
180 struct in6_pktinfo *pi;
182 sndmhdr.msg_name = (caddr_t)&sin6_allrouters;
183 sndmhdr.msg_iov[0].iov_base = (caddr_t)ifinfo->rs_data;
184 sndmhdr.msg_iov[0].iov_len = ifinfo->rs_datalen;
186 cm = CMSG_FIRSTHDR(&sndmhdr);
187 /* specify the outgoing interface */
188 cm->cmsg_level = IPPROTO_IPV6;
189 cm->cmsg_type = IPV6_PKTINFO;
190 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
191 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
192 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
193 pi->ipi6_ifindex = ifinfo->sdl->sdl_index;
195 /* specify the hop limit of the packet */
199 cm = CMSG_NXTHDR(&sndmhdr, cm);
200 cm->cmsg_level = IPPROTO_IPV6;
201 cm->cmsg_type = IPV6_HOPLIMIT;
202 cm->cmsg_len = CMSG_LEN(sizeof(int));
203 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
207 __FUNCTION__, "send RS on %s, whose state is %d",
208 ifinfo->ifname, ifinfo->state);
210 i = sendmsg(rssock, &sndmhdr, 0);
212 if (i < 0 || i != ifinfo->rs_datalen) {
214 * ENETDOWN is not so serious, especially when using several
215 * network cards on a mobile node. We ignore it.
217 if (errno != ENETDOWN || dflag > 0)
218 warnmsg(LOG_ERR, __FUNCTION__, "sendmsg on %s: %s",
219 ifinfo->ifname, strerror(errno));
231 struct icmp6_hdr *icp;
234 struct in6_pktinfo *pi = NULL;
235 struct ifinfo *ifi = NULL;
236 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
239 if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) {
240 warnmsg(LOG_ERR, __FUNCTION__, "recvmsg: %s", strerror(errno));
244 /* extract optional information via Advanced API */
245 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
247 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
248 if (cm->cmsg_level == IPPROTO_IPV6 &&
249 cm->cmsg_type == IPV6_PKTINFO &&
250 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
251 pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
252 ifindex = pi->ipi6_ifindex;
254 if (cm->cmsg_level == IPPROTO_IPV6 &&
255 cm->cmsg_type == IPV6_HOPLIMIT &&
256 cm->cmsg_len == CMSG_LEN(sizeof(int)))
257 hlimp = (int *)CMSG_DATA(cm);
262 __FUNCTION__, "failed to get receiving interface");
267 __FUNCTION__, "failed to get receiving hop limit");
271 if (i < sizeof(struct nd_router_advert)) {
273 __FUNCTION__, "packet size(%d) is too short", i);
277 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
279 if (icp->icmp6_type != ND_ROUTER_ADVERT) {
280 warnmsg(LOG_ERR, __FUNCTION__,
281 "invalid icmp type(%d) from %s on %s", icp->icmp6_type,
282 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
284 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
288 if (icp->icmp6_code != 0) {
289 warnmsg(LOG_ERR, __FUNCTION__,
290 "invalid icmp code(%d) from %s on %s", icp->icmp6_code,
291 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
293 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
298 warnmsg(LOG_NOTICE, __FUNCTION__,
299 "invalid RA with hop limit(%d) from %s on %s",
301 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
303 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
307 if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
308 warnmsg(LOG_NOTICE, __FUNCTION__,
309 "invalid RA with non link-local source from %s on %s",
310 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
312 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
316 /* xxx: more validation? */
318 if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) {
319 warnmsg(LOG_NOTICE, __FUNCTION__,
320 "received RA from %s on an unexpeced IF(%s)",
321 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
323 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
327 warnmsg(LOG_DEBUG, __FUNCTION__,
328 "received RA from %s on %s, state is %d",
329 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
331 ifi->ifname, ifi->state);
336 case IFS_IDLE: /* should be ignored */
337 case IFS_DELAY: /* right? */
340 ifi->state = IFS_IDLE;
342 rtsol_timer_update(ifi);