1 /* $KAME: rtsol.c,v 1.12 2001/11/12 11:47:11 jinmei Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 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$ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/socket.h> 36 #include <sys/uio.h> 37 #include <sys/time.h> 38 #include <sys/queue.h> 39 40 #include <net/if.h> 41 #include <net/route.h> 42 #include <net/if_dl.h> 43 44 #include <netinet/in.h> 45 #include <netinet/ip6.h> 46 #include <netinet6/ip6_var.h> 47 #include <netinet/icmp6.h> 48 49 #include <arpa/inet.h> 50 51 #include <time.h> 52 #include <unistd.h> 53 #include <stdio.h> 54 #include <err.h> 55 #include <errno.h> 56 #include <string.h> 57 #include <stdlib.h> 58 #include <syslog.h> 59 #include "rtsold.h" 60 61 #define ALLROUTER "ff02::2" 62 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; 68 69 int rssock; 70 71 static struct sockaddr_in6 sin6_allrouters = {sizeof(sin6_allrouters), AF_INET6}; 72 73 int 74 sockopen() 75 { 76 int on; 77 struct icmp6_filter filt; 78 static u_char answer[1500]; 79 int rcvcmsglen, sndcmsglen; 80 static u_char *rcvcmsgbuf = NULL, *sndcmsgbuf = NULL; 81 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"); 87 return(-1); 88 } 89 if (sndcmsgbuf == NULL && (sndcmsgbuf = malloc(sndcmsglen)) == NULL) { 90 warnmsg(LOG_ERR, __FUNCTION__, 91 "malloc for send msghdr failed"); 92 return(-1); 93 } 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", 100 ALLROUTER); 101 return(-1); 102 } 103 104 if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { 105 warnmsg(LOG_ERR, __FUNCTION__, "socket: %s", strerror(errno)); 106 return(-1); 107 } 108 109 /* specify to tell receiving interface */ 110 on = 1; 111 #ifdef IPV6_RECVPKTINFO 112 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, 113 sizeof(on)) < 0) { 114 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_RECVPKTINFO: %s", 115 strerror(errno)); 116 exit(1); 117 } 118 #else /* old adv. API */ 119 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_PKTINFO, &on, 120 sizeof(on)) < 0) { 121 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_PKTINFO: %s", 122 strerror(errno)); 123 exit(1); 124 } 125 #endif 126 127 on = 1; 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, 131 sizeof(on)) < 0) { 132 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_RECVHOPLIMIT: %s", 133 strerror(errno)); 134 exit(1); 135 } 136 #else /* old adv. API */ 137 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, 138 sizeof(on)) < 0) { 139 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_HOPLIMIT: %s", 140 strerror(errno)); 141 exit(1); 142 } 143 #endif 144 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", 151 strerror(errno)); 152 return(-1); 153 } 154 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; 164 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; 171 172 return(rssock); 173 } 174 175 void 176 sendpacket(struct ifinfo *ifinfo) 177 { 178 int i; 179 struct cmsghdr *cm; 180 struct in6_pktinfo *pi; 181 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; 185 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; 194 195 /* specify the hop limit of the packet */ 196 { 197 int hoplimit = 255; 198 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)); 204 } 205 206 warnmsg(LOG_DEBUG, 207 __FUNCTION__, "send RS on %s, whose state is %d", 208 ifinfo->ifname, ifinfo->state); 209 210 i = sendmsg(rssock, &sndmhdr, 0); 211 212 if (i < 0 || i != ifinfo->rs_datalen) { 213 /* 214 * ENETDOWN is not so serious, especially when using several 215 * network cards on a mobile node. We ignore it. 216 */ 217 if (errno != ENETDOWN || dflag > 0) 218 warnmsg(LOG_ERR, __FUNCTION__, "sendmsg on %s: %s", 219 ifinfo->ifname, strerror(errno)); 220 } 221 222 /* update counter */ 223 ifinfo->probes++; 224 } 225 226 void 227 rtsol_input(int s) 228 { 229 int i; 230 int *hlimp = NULL; 231 struct icmp6_hdr *icp; 232 int ifindex = 0; 233 struct cmsghdr *cm; 234 struct in6_pktinfo *pi = NULL; 235 struct ifinfo *ifi = NULL; 236 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ]; 237 238 /* get message */ 239 if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) { 240 warnmsg(LOG_ERR, __FUNCTION__, "recvmsg: %s", strerror(errno)); 241 return; 242 } 243 244 /* extract optional information via Advanced API */ 245 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr); 246 cm; 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; 253 } 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); 258 } 259 260 if (ifindex == 0) { 261 warnmsg(LOG_ERR, 262 __FUNCTION__, "failed to get receiving interface"); 263 return; 264 } 265 if (hlimp == NULL) { 266 warnmsg(LOG_ERR, 267 __FUNCTION__, "failed to get receiving hop limit"); 268 return; 269 } 270 271 if (i < sizeof(struct nd_router_advert)) { 272 warnmsg(LOG_ERR, 273 __FUNCTION__, "packet size(%d) is too short", i); 274 return; 275 } 276 277 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base; 278 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, 283 INET6_ADDRSTRLEN), 284 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 285 return; 286 } 287 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, 292 INET6_ADDRSTRLEN), 293 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 294 return; 295 } 296 297 if (*hlimp != 255) { 298 warnmsg(LOG_NOTICE, __FUNCTION__, 299 "invalid RA with hop limit(%d) from %s on %s", 300 *hlimp, 301 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 302 INET6_ADDRSTRLEN), 303 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 304 return; 305 } 306 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, 311 INET6_ADDRSTRLEN), 312 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 313 return; 314 } 315 316 /* xxx: more validation? */ 317 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, 322 INET6_ADDRSTRLEN), 323 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 324 return; 325 } 326 327 warnmsg(LOG_DEBUG, __FUNCTION__, 328 "received RA from %s on %s, state is %d", 329 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 330 INET6_ADDRSTRLEN), 331 ifi->ifname, ifi->state); 332 333 ifi->racnt++; 334 335 switch(ifi->state) { 336 case IFS_IDLE: /* should be ignored */ 337 case IFS_DELAY: /* right? */ 338 break; 339 case IFS_PROBE: 340 ifi->state = IFS_IDLE; 341 ifi->probes = 0; 342 rtsol_timer_update(ifi); 343 break; 344 } 345 } 346